blob: e807edc5126a83a23409b3131c4a9bca56e35751 [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();
Bill Wendling722b26c2012-10-14 07:35:59 +0000335 PAL = PAL.addAttr(getContext(), 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();
Bill Wendling85a64c22012-10-14 06:39:53 +0000341 PAL = PAL.removeAttr(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000342 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000343}
344
Bill Wendlingff758fb2012-10-09 21:49:51 +0000345bool CallInst::hasFnAttr(Attributes::AttrVal A) const {
346 if (AttributeList.getParamAttributes(~0U).hasAttribute(A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000347 return true;
348 if (const Function *F = getCalledFunction())
Bill Wendlingff758fb2012-10-09 21:49:51 +0000349 return F->getParamAttributes(~0U).hasAttribute(A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000350 return false;
351}
352
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000353bool CallInst::paramHasAttr(unsigned i, Attributes::AttrVal A) const {
354 if (AttributeList.getParamAttributes(i).hasAttribute(A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000355 return true;
356 if (const Function *F = getCalledFunction())
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000357 return F->getParamAttributes(i).hasAttribute(A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000358 return false;
359}
360
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000361/// IsConstantOne - Return true only if val is constant int 1
362static bool IsConstantOne(Value *val) {
363 assert(val && "IsConstantOne does not work with NULL val");
364 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
365}
366
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000367static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000368 BasicBlock *InsertAtEnd, Type *IntPtrTy,
369 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000370 Value *ArraySize, Function *MallocF,
371 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000372 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000373 "createMalloc needs either InsertBefore or InsertAtEnd");
374
375 // malloc(type) becomes:
376 // bitcast (i8* malloc(typeSize)) to type*
377 // malloc(type, arraySize) becomes:
378 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000379 if (!ArraySize)
380 ArraySize = ConstantInt::get(IntPtrTy, 1);
381 else if (ArraySize->getType() != IntPtrTy) {
382 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000383 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
384 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000385 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000386 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
387 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000388 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000389
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000390 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000391 if (IsConstantOne(AllocSize)) {
392 AllocSize = ArraySize; // Operand * 1 = Operand
393 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
394 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
395 false /*ZExt*/);
396 // Malloc arg is constant product of type size and array size
397 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
398 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000399 // Multiply type size by the array size...
400 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000401 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
402 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000403 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000404 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
405 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000407 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408
Victor Hernandez788eaab2009-09-18 19:20:02 +0000409 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000410 // Create the call to Malloc.
411 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
412 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000413 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000414 Value *MallocFunc = MallocF;
415 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000416 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000417 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000418 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000419 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000420 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000421 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000422 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000423 Result = MCall;
424 if (Result->getType() != AllocPtrType)
425 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000426 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000427 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000428 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000429 Result = MCall;
430 if (Result->getType() != AllocPtrType) {
431 InsertAtEnd->getInstList().push_back(MCall);
432 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000433 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000434 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000435 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000436 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000437 if (Function *F = dyn_cast<Function>(MallocFunc)) {
438 MCall->setCallingConv(F->getCallingConv());
439 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
440 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000441 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000442
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000443 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000444}
445
446/// CreateMalloc - Generate the IR for a call to malloc:
447/// 1. Compute the malloc call's argument as the specified type's size,
448/// possibly multiplied by the array size if the array size is not
449/// constant 1.
450/// 2. Call malloc with that argument.
451/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000452Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000453 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000454 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000455 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000456 const Twine &Name) {
457 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000458 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000459}
460
461/// CreateMalloc - Generate the IR for a call to malloc:
462/// 1. Compute the malloc call's argument as the specified type's size,
463/// possibly multiplied by the array size if the array size is not
464/// constant 1.
465/// 2. Call malloc with that argument.
466/// 3. Bitcast the result of the malloc call to the specified type.
467/// Note: This function does not add the bitcast to the basic block, that is the
468/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000469Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000470 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000471 Value *AllocSize, Value *ArraySize,
472 Function *MallocF, const Twine &Name) {
473 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000474 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000475}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000476
Victor Hernandeze2971492009-10-24 04:23:03 +0000477static Instruction* createFree(Value* Source, Instruction *InsertBefore,
478 BasicBlock *InsertAtEnd) {
479 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
480 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000481 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000482 "Can not free something of nonpointer type!");
483
484 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
485 Module* M = BB->getParent()->getParent();
486
Chris Lattner229907c2011-07-18 04:54:35 +0000487 Type *VoidTy = Type::getVoidTy(M->getContext());
488 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000489 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000490 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000491 CallInst* Result = NULL;
492 Value *PtrCast = Source;
493 if (InsertBefore) {
494 if (Source->getType() != IntPtrTy)
495 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
496 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
497 } else {
498 if (Source->getType() != IntPtrTy)
499 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
500 Result = CallInst::Create(FreeFunc, PtrCast, "");
501 }
502 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000503 if (Function *F = dyn_cast<Function>(FreeFunc))
504 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000505
506 return Result;
507}
508
509/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000510Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
511 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000512}
513
514/// CreateFree - Generate the IR for a call to the builtin free function.
515/// Note: This function does not add the call to the basic block, that is the
516/// responsibility of the caller.
517Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
518 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
519 assert(FreeCall && "CreateFree did not create a CallInst");
520 return FreeCall;
521}
522
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000523//===----------------------------------------------------------------------===//
524// InvokeInst Implementation
525//===----------------------------------------------------------------------===//
526
527void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000528 ArrayRef<Value *> Args, const Twine &NameStr) {
529 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000530 Op<-3>() = Fn;
531 Op<-2>() = IfNormal;
532 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000533
534#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000535 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000536 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000537
Jay Foad5bd375a2011-07-15 08:37:34 +0000538 assert(((Args.size() == FTy->getNumParams()) ||
539 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000540 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000541
Jay Foad5bd375a2011-07-15 08:37:34 +0000542 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000543 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000544 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000545 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000546#endif
547
548 std::copy(Args.begin(), Args.end(), op_begin());
549 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000550}
551
Misha Brukmanb1c93172005-04-21 23:48:37 +0000552InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000553 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000554 OperandTraits<InvokeInst>::op_end(this)
555 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000556 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000557 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000558 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000559 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000560 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000561}
562
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000563BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
564 return getSuccessor(idx);
565}
566unsigned InvokeInst::getNumSuccessorsV() const {
567 return getNumSuccessors();
568}
569void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
570 return setSuccessor(idx, B);
571}
572
Bill Wendlingff758fb2012-10-09 21:49:51 +0000573bool InvokeInst::hasFnAttr(Attributes::AttrVal A) const {
574 if (AttributeList.getParamAttributes(~0U).hasAttribute(A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000575 return true;
576 if (const Function *F = getCalledFunction())
Bill Wendlingff758fb2012-10-09 21:49:51 +0000577 return F->getParamAttributes(~0U).hasAttribute(A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000578 return false;
579}
580
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000581bool InvokeInst::paramHasAttr(unsigned i, Attributes::AttrVal A) const {
582 if (AttributeList.getParamAttributes(i).hasAttribute(A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000583 return true;
584 if (const Function *F = getCalledFunction())
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000585 return F->getParamAttributes(i).hasAttribute(A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000586 return false;
587}
588
Devang Patel4c758ea2008-09-25 21:00:45 +0000589void InvokeInst::addAttribute(unsigned i, Attributes attr) {
590 AttrListPtr PAL = getAttributes();
Bill Wendling722b26c2012-10-14 07:35:59 +0000591 PAL = PAL.addAttr(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000592 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000593}
594
Devang Patel4c758ea2008-09-25 21:00:45 +0000595void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
596 AttrListPtr PAL = getAttributes();
Bill Wendling85a64c22012-10-14 06:39:53 +0000597 PAL = PAL.removeAttr(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000598 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000599}
600
Bill Wendlingfae14752011-08-12 20:24:12 +0000601LandingPadInst *InvokeInst::getLandingPadInst() const {
602 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
603}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000604
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000605//===----------------------------------------------------------------------===//
606// ReturnInst Implementation
607//===----------------------------------------------------------------------===//
608
Chris Lattner2195fc42007-02-24 00:55:48 +0000609ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000610 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000611 OperandTraits<ReturnInst>::op_end(this) -
612 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000613 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000614 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000615 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000616 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000617}
618
Owen Anderson55f1c092009-08-13 21:58:54 +0000619ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
620 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000621 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
622 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000623 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000624 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000625}
Owen Anderson55f1c092009-08-13 21:58:54 +0000626ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
627 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000628 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
629 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000630 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000631 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000632}
Owen Anderson55f1c092009-08-13 21:58:54 +0000633ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
634 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000635 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000636}
637
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638unsigned ReturnInst::getNumSuccessorsV() const {
639 return getNumSuccessors();
640}
641
Devang Patelae682fb2008-02-26 17:56:20 +0000642/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
643/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000644void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000645 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000646}
647
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000649 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000650}
651
Devang Patel59643e52008-02-23 00:35:18 +0000652ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000653}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000655//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000656// ResumeInst Implementation
657//===----------------------------------------------------------------------===//
658
659ResumeInst::ResumeInst(const ResumeInst &RI)
660 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
661 OperandTraits<ResumeInst>::op_begin(this), 1) {
662 Op<0>() = RI.Op<0>();
663}
664
665ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
666 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
667 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
668 Op<0>() = Exn;
669}
670
671ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
672 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
673 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
674 Op<0>() = Exn;
675}
676
677unsigned ResumeInst::getNumSuccessorsV() const {
678 return getNumSuccessors();
679}
680
681void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
682 llvm_unreachable("ResumeInst has no successors!");
683}
684
685BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
686 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000687}
688
689//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000690// UnreachableInst Implementation
691//===----------------------------------------------------------------------===//
692
Owen Anderson55f1c092009-08-13 21:58:54 +0000693UnreachableInst::UnreachableInst(LLVMContext &Context,
694 Instruction *InsertBefore)
695 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
696 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000697}
Owen Anderson55f1c092009-08-13 21:58:54 +0000698UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
699 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
700 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000701}
702
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000703unsigned UnreachableInst::getNumSuccessorsV() const {
704 return getNumSuccessors();
705}
706
707void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000708 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000709}
710
711BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000712 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000713}
714
715//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000716// BranchInst Implementation
717//===----------------------------------------------------------------------===//
718
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000719void BranchInst::AssertOK() {
720 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000721 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000722 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723}
724
Chris Lattner2195fc42007-02-24 00:55:48 +0000725BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000726 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000727 OperandTraits<BranchInst>::op_end(this) - 1,
728 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000729 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000730 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000731}
732BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
733 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000734 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000735 OperandTraits<BranchInst>::op_end(this) - 3,
736 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000737 Op<-1>() = IfTrue;
738 Op<-2>() = IfFalse;
739 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000740#ifndef NDEBUG
741 AssertOK();
742#endif
743}
744
745BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000746 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000747 OperandTraits<BranchInst>::op_end(this) - 1,
748 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000749 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000750 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000751}
752
753BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
754 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000755 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000756 OperandTraits<BranchInst>::op_end(this) - 3,
757 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000758 Op<-1>() = IfTrue;
759 Op<-2>() = IfFalse;
760 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000761#ifndef NDEBUG
762 AssertOK();
763#endif
764}
765
766
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000767BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000768 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000769 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
770 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000771 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000772 if (BI.getNumOperands() != 1) {
773 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000774 Op<-3>() = BI.Op<-3>();
775 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000777 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000778}
779
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000780void BranchInst::swapSuccessors() {
781 assert(isConditional() &&
782 "Cannot swap successors of an unconditional branch");
783 Op<-1>().swap(Op<-2>());
784
785 // Update profile metadata if present and it matches our structural
786 // expectations.
787 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
788 if (!ProfileData || ProfileData->getNumOperands() != 3)
789 return;
790
791 // The first operand is the name. Fetch them backwards and build a new one.
792 Value *Ops[] = {
793 ProfileData->getOperand(0),
794 ProfileData->getOperand(2),
795 ProfileData->getOperand(1)
796 };
797 setMetadata(LLVMContext::MD_prof,
798 MDNode::get(ProfileData->getContext(), Ops));
799}
800
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000801BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
802 return getSuccessor(idx);
803}
804unsigned BranchInst::getNumSuccessorsV() const {
805 return getNumSuccessors();
806}
807void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
808 setSuccessor(idx, B);
809}
810
811
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000813// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000814//===----------------------------------------------------------------------===//
815
Owen Andersonb6b25302009-07-14 23:09:55 +0000816static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000817 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000818 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000819 else {
820 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000821 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000822 assert(Amt->getType()->isIntegerTy() &&
823 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000824 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000825 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000826}
827
Chris Lattner229907c2011-07-18 04:54:35 +0000828AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000829 const Twine &Name, Instruction *InsertBefore)
830 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
831 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
832 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000833 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000834 setName(Name);
835}
836
Chris Lattner229907c2011-07-18 04:54:35 +0000837AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000838 const Twine &Name, BasicBlock *InsertAtEnd)
839 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
840 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
841 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000842 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000843 setName(Name);
844}
845
Chris Lattner229907c2011-07-18 04:54:35 +0000846AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000847 Instruction *InsertBefore)
848 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
849 getAISize(Ty->getContext(), 0), InsertBefore) {
850 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000851 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000852 setName(Name);
853}
854
Chris Lattner229907c2011-07-18 04:54:35 +0000855AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000856 BasicBlock *InsertAtEnd)
857 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
858 getAISize(Ty->getContext(), 0), InsertAtEnd) {
859 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000860 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000861 setName(Name);
862}
863
Chris Lattner229907c2011-07-18 04:54:35 +0000864AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000865 const Twine &Name, Instruction *InsertBefore)
866 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000867 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000868 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000869 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000870 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000871}
872
Chris Lattner229907c2011-07-18 04:54:35 +0000873AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000874 const Twine &Name, BasicBlock *InsertAtEnd)
875 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000876 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000877 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000878 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000879 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880}
881
Gordon Henriksen14a55692007-12-10 02:14:30 +0000882// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000883AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000884}
885
Victor Hernandez8acf2952009-10-23 21:09:37 +0000886void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000887 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000888 assert(Align <= MaximumAlignment &&
889 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000890 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000891 assert(getAlignment() == Align && "Alignment representation error!");
892}
893
Victor Hernandez8acf2952009-10-23 21:09:37 +0000894bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000895 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000896 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000897 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898}
899
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000900Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901 return getType()->getElementType();
902}
903
Chris Lattner8b291e62008-11-26 02:54:17 +0000904/// isStaticAlloca - Return true if this alloca is in the entry block of the
905/// function and is a constant size. If so, the code generator will fold it
906/// into the prolog/epilog code, so it is basically free.
907bool AllocaInst::isStaticAlloca() const {
908 // Must be constant size.
909 if (!isa<ConstantInt>(getArraySize())) return false;
910
911 // Must be in the entry block.
912 const BasicBlock *Parent = getParent();
913 return Parent == &Parent->getParent()->front();
914}
915
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000916//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000917// LoadInst Implementation
918//===----------------------------------------------------------------------===//
919
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000920void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000921 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000923 assert(!(isAtomic() && getAlignment() == 0) &&
924 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000925}
926
Daniel Dunbar4975db62009-07-25 04:41:11 +0000927LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000928 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000929 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000930 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000931 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000932 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000933 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000934 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000935}
936
Daniel Dunbar4975db62009-07-25 04:41:11 +0000937LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000938 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000939 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000940 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000941 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000942 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000943 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000944 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000945}
946
Daniel Dunbar4975db62009-07-25 04:41:11 +0000947LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000948 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000949 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000950 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000951 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000952 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000953 setAtomic(NotAtomic);
954 AssertOK();
955 setName(Name);
956}
957
958LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
959 BasicBlock *InsertAE)
960 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
961 Load, Ptr, InsertAE) {
962 setVolatile(isVolatile);
963 setAlignment(0);
964 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +0000965 AssertOK();
966 setName(Name);
967}
968
Daniel Dunbar4975db62009-07-25 04:41:11 +0000969LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000970 unsigned Align, Instruction *InsertBef)
971 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
972 Load, Ptr, InsertBef) {
973 setVolatile(isVolatile);
974 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000975 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000976 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000977 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000978}
979
Daniel Dunbar4975db62009-07-25 04:41:11 +0000980LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000981 unsigned Align, BasicBlock *InsertAE)
982 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
983 Load, Ptr, InsertAE) {
984 setVolatile(isVolatile);
985 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000986 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +0000987 AssertOK();
988 setName(Name);
989}
990
Eli Friedman59b66882011-08-09 23:02:53 +0000991LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
992 unsigned Align, AtomicOrdering Order,
993 SynchronizationScope SynchScope,
994 Instruction *InsertBef)
995 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
996 Load, Ptr, InsertBef) {
997 setVolatile(isVolatile);
998 setAlignment(Align);
999 setAtomic(Order, SynchScope);
1000 AssertOK();
1001 setName(Name);
1002}
1003
1004LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1005 unsigned Align, AtomicOrdering Order,
1006 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001007 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001008 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001009 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001010 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001011 setAlignment(Align);
1012 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001013 AssertOK();
1014 setName(Name);
1015}
1016
Daniel Dunbar27096822009-08-11 18:11:15 +00001017LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1018 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1019 Load, Ptr, InsertBef) {
1020 setVolatile(false);
1021 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001022 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001023 AssertOK();
1024 if (Name && Name[0]) setName(Name);
1025}
1026
1027LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1028 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1029 Load, Ptr, InsertAE) {
1030 setVolatile(false);
1031 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001032 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001033 AssertOK();
1034 if (Name && Name[0]) setName(Name);
1035}
1036
1037LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1038 Instruction *InsertBef)
1039: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1040 Load, Ptr, InsertBef) {
1041 setVolatile(isVolatile);
1042 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001043 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001044 AssertOK();
1045 if (Name && Name[0]) setName(Name);
1046}
1047
1048LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1049 BasicBlock *InsertAE)
1050 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1051 Load, Ptr, InsertAE) {
1052 setVolatile(isVolatile);
1053 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001054 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001055 AssertOK();
1056 if (Name && Name[0]) setName(Name);
1057}
1058
Christopher Lamb84485702007-04-22 19:24:39 +00001059void LoadInst::setAlignment(unsigned Align) {
1060 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001061 assert(Align <= MaximumAlignment &&
1062 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001063 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001064 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001065 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001066}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001067
1068//===----------------------------------------------------------------------===//
1069// StoreInst Implementation
1070//===----------------------------------------------------------------------===//
1071
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001072void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001073 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001074 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001075 "Ptr must have pointer type!");
1076 assert(getOperand(0)->getType() ==
1077 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001078 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001079 assert(!(isAtomic() && getAlignment() == 0) &&
1080 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001081}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001082
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001083
1084StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001085 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001086 OperandTraits<StoreInst>::op_begin(this),
1087 OperandTraits<StoreInst>::operands(this),
1088 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001089 Op<0>() = val;
1090 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001091 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001092 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001093 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001094 AssertOK();
1095}
1096
1097StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001098 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001099 OperandTraits<StoreInst>::op_begin(this),
1100 OperandTraits<StoreInst>::operands(this),
1101 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001102 Op<0>() = val;
1103 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001104 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001105 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001106 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001107 AssertOK();
1108}
1109
Misha Brukmanb1c93172005-04-21 23:48:37 +00001110StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001111 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001112 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001113 OperandTraits<StoreInst>::op_begin(this),
1114 OperandTraits<StoreInst>::operands(this),
1115 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001116 Op<0>() = val;
1117 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001118 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001119 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001120 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001121 AssertOK();
1122}
1123
1124StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1125 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001126 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001127 OperandTraits<StoreInst>::op_begin(this),
1128 OperandTraits<StoreInst>::operands(this),
1129 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001130 Op<0>() = val;
1131 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001132 setVolatile(isVolatile);
1133 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001134 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001135 AssertOK();
1136}
1137
Misha Brukmanb1c93172005-04-21 23:48:37 +00001138StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001139 unsigned Align, AtomicOrdering Order,
1140 SynchronizationScope SynchScope,
1141 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001142 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001143 OperandTraits<StoreInst>::op_begin(this),
1144 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001145 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001146 Op<0>() = val;
1147 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001148 setVolatile(isVolatile);
1149 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001150 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001151 AssertOK();
1152}
1153
1154StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001155 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001156 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001157 OperandTraits<StoreInst>::op_begin(this),
1158 OperandTraits<StoreInst>::operands(this),
1159 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001160 Op<0>() = val;
1161 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001162 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001163 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001164 setAtomic(NotAtomic);
1165 AssertOK();
1166}
1167
1168StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1169 unsigned Align, BasicBlock *InsertAtEnd)
1170 : Instruction(Type::getVoidTy(val->getContext()), Store,
1171 OperandTraits<StoreInst>::op_begin(this),
1172 OperandTraits<StoreInst>::operands(this),
1173 InsertAtEnd) {
1174 Op<0>() = val;
1175 Op<1>() = addr;
1176 setVolatile(isVolatile);
1177 setAlignment(Align);
1178 setAtomic(NotAtomic);
1179 AssertOK();
1180}
1181
1182StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1183 unsigned Align, AtomicOrdering Order,
1184 SynchronizationScope SynchScope,
1185 BasicBlock *InsertAtEnd)
1186 : Instruction(Type::getVoidTy(val->getContext()), Store,
1187 OperandTraits<StoreInst>::op_begin(this),
1188 OperandTraits<StoreInst>::operands(this),
1189 InsertAtEnd) {
1190 Op<0>() = val;
1191 Op<1>() = addr;
1192 setVolatile(isVolatile);
1193 setAlignment(Align);
1194 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001195 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001196}
1197
Christopher Lamb84485702007-04-22 19:24:39 +00001198void StoreInst::setAlignment(unsigned Align) {
1199 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001200 assert(Align <= MaximumAlignment &&
1201 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001202 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001203 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001204 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001205}
1206
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001207//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001208// AtomicCmpXchgInst Implementation
1209//===----------------------------------------------------------------------===//
1210
1211void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1212 AtomicOrdering Ordering,
1213 SynchronizationScope SynchScope) {
1214 Op<0>() = Ptr;
1215 Op<1>() = Cmp;
1216 Op<2>() = NewVal;
1217 setOrdering(Ordering);
1218 setSynchScope(SynchScope);
1219
1220 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1221 "All operands must be non-null!");
1222 assert(getOperand(0)->getType()->isPointerTy() &&
1223 "Ptr must have pointer type!");
1224 assert(getOperand(1)->getType() ==
1225 cast<PointerType>(getOperand(0)->getType())->getElementType()
1226 && "Ptr must be a pointer to Cmp type!");
1227 assert(getOperand(2)->getType() ==
1228 cast<PointerType>(getOperand(0)->getType())->getElementType()
1229 && "Ptr must be a pointer to NewVal type!");
1230 assert(Ordering != NotAtomic &&
1231 "AtomicCmpXchg instructions must be atomic!");
1232}
1233
1234AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1235 AtomicOrdering Ordering,
1236 SynchronizationScope SynchScope,
1237 Instruction *InsertBefore)
1238 : Instruction(Cmp->getType(), AtomicCmpXchg,
1239 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1240 OperandTraits<AtomicCmpXchgInst>::operands(this),
1241 InsertBefore) {
1242 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1243}
1244
1245AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1246 AtomicOrdering Ordering,
1247 SynchronizationScope SynchScope,
1248 BasicBlock *InsertAtEnd)
1249 : Instruction(Cmp->getType(), AtomicCmpXchg,
1250 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1251 OperandTraits<AtomicCmpXchgInst>::operands(this),
1252 InsertAtEnd) {
1253 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1254}
1255
1256//===----------------------------------------------------------------------===//
1257// AtomicRMWInst Implementation
1258//===----------------------------------------------------------------------===//
1259
1260void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1261 AtomicOrdering Ordering,
1262 SynchronizationScope SynchScope) {
1263 Op<0>() = Ptr;
1264 Op<1>() = Val;
1265 setOperation(Operation);
1266 setOrdering(Ordering);
1267 setSynchScope(SynchScope);
1268
1269 assert(getOperand(0) && getOperand(1) &&
1270 "All operands must be non-null!");
1271 assert(getOperand(0)->getType()->isPointerTy() &&
1272 "Ptr must have pointer type!");
1273 assert(getOperand(1)->getType() ==
1274 cast<PointerType>(getOperand(0)->getType())->getElementType()
1275 && "Ptr must be a pointer to Val type!");
1276 assert(Ordering != NotAtomic &&
1277 "AtomicRMW instructions must be atomic!");
1278}
1279
1280AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1281 AtomicOrdering Ordering,
1282 SynchronizationScope SynchScope,
1283 Instruction *InsertBefore)
1284 : Instruction(Val->getType(), AtomicRMW,
1285 OperandTraits<AtomicRMWInst>::op_begin(this),
1286 OperandTraits<AtomicRMWInst>::operands(this),
1287 InsertBefore) {
1288 Init(Operation, Ptr, Val, Ordering, SynchScope);
1289}
1290
1291AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1292 AtomicOrdering Ordering,
1293 SynchronizationScope SynchScope,
1294 BasicBlock *InsertAtEnd)
1295 : Instruction(Val->getType(), AtomicRMW,
1296 OperandTraits<AtomicRMWInst>::op_begin(this),
1297 OperandTraits<AtomicRMWInst>::operands(this),
1298 InsertAtEnd) {
1299 Init(Operation, Ptr, Val, Ordering, SynchScope);
1300}
1301
1302//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001303// FenceInst Implementation
1304//===----------------------------------------------------------------------===//
1305
1306FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1307 SynchronizationScope SynchScope,
1308 Instruction *InsertBefore)
1309 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1310 setOrdering(Ordering);
1311 setSynchScope(SynchScope);
1312}
1313
1314FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1315 SynchronizationScope SynchScope,
1316 BasicBlock *InsertAtEnd)
1317 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1318 setOrdering(Ordering);
1319 setSynchScope(SynchScope);
1320}
1321
1322//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001323// GetElementPtrInst Implementation
1324//===----------------------------------------------------------------------===//
1325
Jay Foadd1b78492011-07-25 09:48:08 +00001326void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001327 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001328 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1329 OperandList[0] = Ptr;
1330 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001331 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001332}
1333
Gabor Greiff6caff662008-05-10 08:32:32 +00001334GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001335 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001336 OperandTraits<GetElementPtrInst>::op_end(this)
1337 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001338 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001339 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001340 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001341}
1342
Chris Lattner6090a422009-03-09 04:46:40 +00001343/// getIndexedType - Returns the type of the element that would be accessed with
1344/// a gep instruction with the specified parameters.
1345///
1346/// The Idxs pointer should point to a continuous piece of memory containing the
1347/// indices, either as Value* or uint64_t.
1348///
1349/// A null type is returned if the indices are invalid for the specified
1350/// pointer type.
1351///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001352template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001353static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001354 if (Ptr->isVectorTy()) {
1355 assert(IdxList.size() == 1 &&
1356 "GEP with vector pointers must have a single index");
1357 PointerType *PTy = dyn_cast<PointerType>(
1358 cast<VectorType>(Ptr)->getElementType());
1359 assert(PTy && "Gep with invalid vector pointer found");
1360 return PTy->getElementType();
1361 }
1362
Chris Lattner229907c2011-07-18 04:54:35 +00001363 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001364 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001365 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001366
Chris Lattner6090a422009-03-09 04:46:40 +00001367 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001368 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001369 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001370
Chris Lattner6090a422009-03-09 04:46:40 +00001371 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001372 // it cannot be 'stepped over'.
1373 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001374 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001375
Dan Gohman1ecaf452008-05-31 00:58:22 +00001376 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001377 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001378 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001379 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001380 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001381 if (!CT->indexValid(Index)) return 0;
1382 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001383 }
Jay Foadd1b78492011-07-25 09:48:08 +00001384 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001385}
1386
Jay Foadd1b78492011-07-25 09:48:08 +00001387Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1388 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001389}
1390
Chris Lattner229907c2011-07-18 04:54:35 +00001391Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001392 ArrayRef<Constant *> IdxList) {
1393 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001394}
1395
Jay Foadd1b78492011-07-25 09:48:08 +00001396Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1397 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001398}
1399
Nadav Rotem3924cb02011-12-05 06:29:09 +00001400unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) {
1401 Type *Ty = Ptr->getType();
1402
1403 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1404 Ty = VTy->getElementType();
1405
1406 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
1407 return PTy->getAddressSpace();
1408
Craig Topperc514b542012-02-05 22:14:15 +00001409 llvm_unreachable("Invalid GEP pointer type");
Nadav Rotem3924cb02011-12-05 06:29:09 +00001410}
1411
Chris Lattner45f15572007-04-14 00:12:57 +00001412/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1413/// zeros. If so, the result pointer and the first operand have the same
1414/// value, just potentially different types.
1415bool GetElementPtrInst::hasAllZeroIndices() const {
1416 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1417 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1418 if (!CI->isZero()) return false;
1419 } else {
1420 return false;
1421 }
1422 }
1423 return true;
1424}
1425
Chris Lattner27058292007-04-27 20:35:56 +00001426/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1427/// constant integers. If so, the result pointer and the first operand have
1428/// a constant offset between them.
1429bool GetElementPtrInst::hasAllConstantIndices() const {
1430 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1431 if (!isa<ConstantInt>(getOperand(i)))
1432 return false;
1433 }
1434 return true;
1435}
1436
Dan Gohman1b849082009-09-07 23:54:19 +00001437void GetElementPtrInst::setIsInBounds(bool B) {
1438 cast<GEPOperator>(this)->setIsInBounds(B);
1439}
Chris Lattner45f15572007-04-14 00:12:57 +00001440
Nick Lewycky28a5f252009-09-27 21:33:04 +00001441bool GetElementPtrInst::isInBounds() const {
1442 return cast<GEPOperator>(this)->isInBounds();
1443}
1444
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001445//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001446// ExtractElementInst Implementation
1447//===----------------------------------------------------------------------===//
1448
1449ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001450 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001451 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001452 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001453 ExtractElement,
1454 OperandTraits<ExtractElementInst>::op_begin(this),
1455 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001456 assert(isValidOperands(Val, Index) &&
1457 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001458 Op<0>() = Val;
1459 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001460 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001461}
1462
1463ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001464 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001465 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001466 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001467 ExtractElement,
1468 OperandTraits<ExtractElementInst>::op_begin(this),
1469 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001470 assert(isValidOperands(Val, Index) &&
1471 "Invalid extractelement instruction operands!");
1472
Gabor Greif2d3024d2008-05-26 21:33:52 +00001473 Op<0>() = Val;
1474 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001475 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001476}
1477
Chris Lattner65511ff2006-10-05 06:24:58 +00001478
Chris Lattner54865b32006-04-08 04:05:48 +00001479bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001480 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001481 return false;
1482 return true;
1483}
1484
1485
Robert Bocchino23004482006-01-10 19:05:34 +00001486//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001487// InsertElementInst Implementation
1488//===----------------------------------------------------------------------===//
1489
Chris Lattner54865b32006-04-08 04:05:48 +00001490InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001491 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001492 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001493 : Instruction(Vec->getType(), InsertElement,
1494 OperandTraits<InsertElementInst>::op_begin(this),
1495 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001496 assert(isValidOperands(Vec, Elt, Index) &&
1497 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001498 Op<0>() = Vec;
1499 Op<1>() = Elt;
1500 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001501 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001502}
1503
Chris Lattner54865b32006-04-08 04:05:48 +00001504InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001505 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001506 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001507 : Instruction(Vec->getType(), InsertElement,
1508 OperandTraits<InsertElementInst>::op_begin(this),
1509 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001510 assert(isValidOperands(Vec, Elt, Index) &&
1511 "Invalid insertelement instruction operands!");
1512
Gabor Greif2d3024d2008-05-26 21:33:52 +00001513 Op<0>() = Vec;
1514 Op<1>() = Elt;
1515 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001516 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001517}
1518
Chris Lattner54865b32006-04-08 04:05:48 +00001519bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1520 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001521 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001522 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001523
Reid Spencerd84d35b2007-02-15 02:26:10 +00001524 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001525 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001526
Duncan Sands9dff9be2010-02-15 16:12:20 +00001527 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001528 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001529 return true;
1530}
1531
1532
Robert Bocchinoca27f032006-01-17 20:07:22 +00001533//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001534// ShuffleVectorInst Implementation
1535//===----------------------------------------------------------------------===//
1536
1537ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001538 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001539 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001540: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001541 cast<VectorType>(Mask->getType())->getNumElements()),
1542 ShuffleVector,
1543 OperandTraits<ShuffleVectorInst>::op_begin(this),
1544 OperandTraits<ShuffleVectorInst>::operands(this),
1545 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001546 assert(isValidOperands(V1, V2, Mask) &&
1547 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001548 Op<0>() = V1;
1549 Op<1>() = V2;
1550 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001551 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001552}
1553
1554ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001555 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001556 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001557: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1558 cast<VectorType>(Mask->getType())->getNumElements()),
1559 ShuffleVector,
1560 OperandTraits<ShuffleVectorInst>::op_begin(this),
1561 OperandTraits<ShuffleVectorInst>::operands(this),
1562 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001563 assert(isValidOperands(V1, V2, Mask) &&
1564 "Invalid shuffle vector instruction operands!");
1565
Gabor Greif2d3024d2008-05-26 21:33:52 +00001566 Op<0>() = V1;
1567 Op<1>() = V2;
1568 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001569 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001570}
1571
Mon P Wang25f01062008-11-10 04:46:22 +00001572bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001573 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001574 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001575 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001576 return false;
1577
Chris Lattner1dcb6542012-01-25 23:49:49 +00001578 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001579 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001580 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001581 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001582
1583 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001584 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1585 return true;
1586
Nate Begeman60a31c32010-08-13 00:16:46 +00001587 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001588 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001589 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001590 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1591 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001592 return false;
1593 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1594 return false;
1595 }
1596 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001597 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001598 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001599
1600 if (const ConstantDataSequential *CDS =
1601 dyn_cast<ConstantDataSequential>(Mask)) {
1602 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1603 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1604 if (CDS->getElementAsInteger(i) >= V1Size*2)
1605 return false;
1606 return true;
1607 }
1608
1609 // The bitcode reader can create a place holder for a forward reference
1610 // used as the shuffle mask. When this occurs, the shuffle mask will
1611 // fall into this case and fail. To avoid this error, do this bit of
1612 // ugliness to allow such a mask pass.
1613 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1614 if (CE->getOpcode() == Instruction::UserOp1)
1615 return true;
1616
1617 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001618}
1619
Chris Lattnerf724e342008-03-02 05:28:33 +00001620/// getMaskValue - Return the index from the shuffle mask for the specified
1621/// output result. This is either -1 if the element is undef or a number less
1622/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001623int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1624 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1625 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001626 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001627 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001628 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001629 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001630 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001631}
1632
Chris Lattner1dcb6542012-01-25 23:49:49 +00001633/// getShuffleMask - Return the full mask for this instruction, where each
1634/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001635void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1636 SmallVectorImpl<int> &Result) {
1637 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001638
Chris Lattnercf129702012-01-26 02:51:13 +00001639 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001640 for (unsigned i = 0; i != NumElts; ++i)
1641 Result.push_back(CDS->getElementAsInteger(i));
1642 return;
1643 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001644 for (unsigned i = 0; i != NumElts; ++i) {
1645 Constant *C = Mask->getAggregateElement(i);
1646 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001647 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001648 }
1649}
1650
1651
Dan Gohman12fce772008-05-15 19:50:34 +00001652//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001653// InsertValueInst Class
1654//===----------------------------------------------------------------------===//
1655
Jay Foad57aa6362011-07-13 10:26:04 +00001656void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1657 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001658 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001659
1660 // There's no fundamental reason why we require at least one index
1661 // (other than weirdness with &*IdxBegin being invalid; see
1662 // getelementptr's init routine for example). But there's no
1663 // present need to support it.
1664 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1665
1666 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001667 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001668 Op<0>() = Agg;
1669 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001670
Jay Foad57aa6362011-07-13 10:26:04 +00001671 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001672 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001673}
1674
1675InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001676 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001677 OperandTraits<InsertValueInst>::op_begin(this), 2),
1678 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001679 Op<0>() = IVI.getOperand(0);
1680 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001681 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001682}
1683
1684//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001685// ExtractValueInst Class
1686//===----------------------------------------------------------------------===//
1687
Jay Foad57aa6362011-07-13 10:26:04 +00001688void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001689 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001690
Jay Foad57aa6362011-07-13 10:26:04 +00001691 // There's no fundamental reason why we require at least one index.
1692 // But there's no present need to support it.
1693 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001694
Jay Foad57aa6362011-07-13 10:26:04 +00001695 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001696 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001697}
1698
1699ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001700 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001701 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001702 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001703}
1704
Dan Gohman12fce772008-05-15 19:50:34 +00001705// getIndexedType - Returns the type of the element that would be extracted
1706// with an extractvalue instruction with the specified parameters.
1707//
1708// A null type is returned if the indices are invalid for the specified
1709// pointer type.
1710//
Chris Lattner229907c2011-07-18 04:54:35 +00001711Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001712 ArrayRef<unsigned> Idxs) {
1713 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001714 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001715 // We can't use CompositeType::indexValid(Index) here.
1716 // indexValid() always returns true for arrays because getelementptr allows
1717 // out-of-bounds indices. Since we don't allow those for extractvalue and
1718 // insertvalue we need to check array indexing manually.
1719 // Since the only other types we can index into are struct types it's just
1720 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001721 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001722 if (Index >= AT->getNumElements())
1723 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001724 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001725 if (Index >= ST->getNumElements())
1726 return 0;
1727 } else {
1728 // Not a valid type to index into.
1729 return 0;
1730 }
1731
1732 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001733 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001734 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001735}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001736
1737//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001738// BinaryOperator Class
1739//===----------------------------------------------------------------------===//
1740
Chris Lattner2195fc42007-02-24 00:55:48 +00001741BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001742 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001743 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001744 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001745 OperandTraits<BinaryOperator>::op_begin(this),
1746 OperandTraits<BinaryOperator>::operands(this),
1747 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001748 Op<0>() = S1;
1749 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001750 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001751 setName(Name);
1752}
1753
1754BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001755 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001756 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001757 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001758 OperandTraits<BinaryOperator>::op_begin(this),
1759 OperandTraits<BinaryOperator>::operands(this),
1760 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001761 Op<0>() = S1;
1762 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001763 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001764 setName(Name);
1765}
1766
1767
1768void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001769 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001770 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001771 assert(LHS->getType() == RHS->getType() &&
1772 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001773#ifndef NDEBUG
1774 switch (iType) {
1775 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001776 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001777 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001778 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001779 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001780 "Tried to create an integer operation on a non-integer type!");
1781 break;
1782 case FAdd: case FSub:
1783 case FMul:
1784 assert(getType() == LHS->getType() &&
1785 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001786 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001787 "Tried to create a floating-point operation on a "
1788 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001789 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001790 case UDiv:
1791 case SDiv:
1792 assert(getType() == LHS->getType() &&
1793 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001794 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001795 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001796 "Incorrect operand type (not integer) for S/UDIV");
1797 break;
1798 case FDiv:
1799 assert(getType() == LHS->getType() &&
1800 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001801 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001802 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001803 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001804 case URem:
1805 case SRem:
1806 assert(getType() == LHS->getType() &&
1807 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001808 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001809 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001810 "Incorrect operand type (not integer) for S/UREM");
1811 break;
1812 case FRem:
1813 assert(getType() == LHS->getType() &&
1814 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001815 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001816 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001817 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001818 case Shl:
1819 case LShr:
1820 case AShr:
1821 assert(getType() == LHS->getType() &&
1822 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001823 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001824 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001825 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001826 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001827 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001828 case And: case Or:
1829 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001830 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001831 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001832 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001833 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001834 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001835 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001836 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001837 default:
1838 break;
1839 }
1840#endif
1841}
1842
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001843BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001844 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001845 Instruction *InsertBefore) {
1846 assert(S1->getType() == S2->getType() &&
1847 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001848 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001849}
1850
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001851BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001852 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001853 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001854 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001855 InsertAtEnd->getInstList().push_back(Res);
1856 return Res;
1857}
1858
Dan Gohman5476cfd2009-08-12 16:23:25 +00001859BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001860 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001861 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001862 return new BinaryOperator(Instruction::Sub,
1863 zero, Op,
1864 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001865}
1866
Dan Gohman5476cfd2009-08-12 16:23:25 +00001867BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001868 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001869 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001870 return new BinaryOperator(Instruction::Sub,
1871 zero, Op,
1872 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001873}
1874
Dan Gohman4ab44202009-12-18 02:58:50 +00001875BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1876 Instruction *InsertBefore) {
1877 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1878 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1879}
1880
1881BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1882 BasicBlock *InsertAtEnd) {
1883 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1884 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1885}
1886
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001887BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1888 Instruction *InsertBefore) {
1889 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1890 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1891}
1892
1893BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1894 BasicBlock *InsertAtEnd) {
1895 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1896 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1897}
1898
Dan Gohman5476cfd2009-08-12 16:23:25 +00001899BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001900 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001901 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001902 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001903 Op->getType(), Name, InsertBefore);
1904}
1905
Dan Gohman5476cfd2009-08-12 16:23:25 +00001906BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001907 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001908 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001909 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001910 Op->getType(), Name, InsertAtEnd);
1911}
1912
Dan Gohman5476cfd2009-08-12 16:23:25 +00001913BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001914 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001915 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001916 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001917 Op->getType(), Name, InsertBefore);
1918}
1919
Dan Gohman5476cfd2009-08-12 16:23:25 +00001920BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001921 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001922 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001923 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001924 Op->getType(), Name, InsertAtEnd);
1925}
1926
1927
1928// isConstantAllOnes - Helper function for several functions below
1929static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001930 if (const Constant *C = dyn_cast<Constant>(V))
1931 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001932 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001933}
1934
Owen Andersonbb2501b2009-07-13 22:18:28 +00001935bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001936 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1937 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001938 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1939 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001940 return false;
1941}
1942
Owen Andersonbb2501b2009-07-13 22:18:28 +00001943bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001944 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1945 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001946 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001947 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001948 return false;
1949}
1950
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001951bool BinaryOperator::isNot(const Value *V) {
1952 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1953 return (Bop->getOpcode() == Instruction::Xor &&
1954 (isConstantAllOnes(Bop->getOperand(1)) ||
1955 isConstantAllOnes(Bop->getOperand(0))));
1956 return false;
1957}
1958
Chris Lattner2c7d1772005-04-24 07:28:37 +00001959Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001960 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001961}
1962
Chris Lattner2c7d1772005-04-24 07:28:37 +00001963const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1964 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001965}
1966
Dan Gohmana5b96452009-06-04 22:49:04 +00001967Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001968 return cast<BinaryOperator>(BinOp)->getOperand(1);
1969}
1970
1971const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1972 return getFNegArgument(const_cast<Value*>(BinOp));
1973}
1974
Chris Lattner2c7d1772005-04-24 07:28:37 +00001975Value *BinaryOperator::getNotArgument(Value *BinOp) {
1976 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1977 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1978 Value *Op0 = BO->getOperand(0);
1979 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001980 if (isConstantAllOnes(Op0)) return Op1;
1981
1982 assert(isConstantAllOnes(Op1));
1983 return Op0;
1984}
1985
Chris Lattner2c7d1772005-04-24 07:28:37 +00001986const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1987 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001988}
1989
1990
1991// swapOperands - Exchange the two operands to this instruction. This
1992// instruction is safe to use on any binary instruction and does not
1993// modify the semantics of the instruction. If the instruction is
1994// order dependent (SetLT f.e.) the opcode is changed.
1995//
1996bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001997 if (!isCommutative())
1998 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001999 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002000 return false;
2001}
2002
Dan Gohman1b849082009-09-07 23:54:19 +00002003void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2004 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2005}
2006
2007void BinaryOperator::setHasNoSignedWrap(bool b) {
2008 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2009}
2010
2011void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002012 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002013}
2014
Nick Lewycky28a5f252009-09-27 21:33:04 +00002015bool BinaryOperator::hasNoUnsignedWrap() const {
2016 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2017}
2018
2019bool BinaryOperator::hasNoSignedWrap() const {
2020 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2021}
2022
2023bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002024 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002025}
2026
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002027//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002028// FPMathOperator Class
2029//===----------------------------------------------------------------------===//
2030
2031/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2032/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002033/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002034float FPMathOperator::getFPAccuracy() const {
2035 const MDNode *MD =
2036 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2037 if (!MD)
2038 return 0.0;
Duncan Sands9af62982012-04-16 19:39:33 +00002039 ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2040 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002041}
2042
2043
2044//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002045// CastInst Class
2046//===----------------------------------------------------------------------===//
2047
David Blaikie3a15e142011-12-01 08:00:17 +00002048void CastInst::anchor() {}
2049
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002050// Just determine if this cast only deals with integral->integral conversion.
2051bool CastInst::isIntegerCast() const {
2052 switch (getOpcode()) {
2053 default: return false;
2054 case Instruction::ZExt:
2055 case Instruction::SExt:
2056 case Instruction::Trunc:
2057 return true;
2058 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002059 return getOperand(0)->getType()->isIntegerTy() &&
2060 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002062}
2063
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002064bool CastInst::isLosslessCast() const {
2065 // Only BitCast can be lossless, exit fast if we're not BitCast
2066 if (getOpcode() != Instruction::BitCast)
2067 return false;
2068
2069 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002070 Type* SrcTy = getOperand(0)->getType();
2071 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002072 if (SrcTy == DstTy)
2073 return true;
2074
Reid Spencer8d9336d2006-12-31 05:26:44 +00002075 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002076 if (SrcTy->isPointerTy())
2077 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002078 return false; // Other types have no identity values
2079}
2080
2081/// This function determines if the CastInst does not require any bits to be
2082/// changed in order to effect the cast. Essentially, it identifies cases where
2083/// no code gen is necessary for the cast, hence the name no-op cast. For
2084/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002085/// # bitcast i32* %x to i8*
2086/// # bitcast <2 x i32> %x to <4 x i16>
2087/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002088/// @brief Determine if the described cast is a no-op.
2089bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002090 Type *SrcTy,
2091 Type *DestTy,
2092 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002093 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002094 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002095 case Instruction::Trunc:
2096 case Instruction::ZExt:
2097 case Instruction::SExt:
2098 case Instruction::FPTrunc:
2099 case Instruction::FPExt:
2100 case Instruction::UIToFP:
2101 case Instruction::SIToFP:
2102 case Instruction::FPToUI:
2103 case Instruction::FPToSI:
2104 return false; // These always modify bits
2105 case Instruction::BitCast:
2106 return true; // BitCast never modifies bits.
2107 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002108 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002109 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002111 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002112 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 }
2114}
2115
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002116/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002117bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002118 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2119}
2120
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121/// This function determines if a pair of casts can be eliminated and what
2122/// opcode should be used in the elimination. This assumes that there are two
2123/// instructions like this:
2124/// * %F = firstOpcode SrcTy %x to MidTy
2125/// * %S = secondOpcode MidTy %F to DstTy
2126/// The function returns a resultOpcode so these two casts can be replaced with:
2127/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2128/// If no such cast is permited, the function returns 0.
2129unsigned CastInst::isEliminableCastPair(
2130 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002131 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002132 // Define the 144 possibilities for these two cast instructions. The values
2133 // in this matrix determine what to do in a given situation and select the
2134 // case in the switch below. The rows correspond to firstOp, the columns
2135 // correspond to secondOp. In looking at the table below, keep in mind
2136 // the following cast properties:
2137 //
2138 // Size Compare Source Destination
2139 // Operator Src ? Size Type Sign Type Sign
2140 // -------- ------------ ------------------- ---------------------
2141 // TRUNC > Integer Any Integral Any
2142 // ZEXT < Integral Unsigned Integer Any
2143 // SEXT < Integral Signed Integer Any
2144 // FPTOUI n/a FloatPt n/a Integral Unsigned
2145 // FPTOSI n/a FloatPt n/a Integral Signed
2146 // UITOFP n/a Integral Unsigned FloatPt n/a
2147 // SITOFP n/a Integral Signed FloatPt n/a
2148 // FPTRUNC > FloatPt n/a FloatPt n/a
2149 // FPEXT < FloatPt n/a FloatPt n/a
2150 // PTRTOINT n/a Pointer n/a Integral Unsigned
2151 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002152 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002153 //
2154 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002155 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2156 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002157 // of the produced value (we no longer know the top-part is all zeros).
2158 // Further this conversion is often much more expensive for typical hardware,
2159 // and causes issues when building libgcc. We disallow fptosi+sext for the
2160 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161 const unsigned numCastOps =
2162 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2163 static const uint8_t CastResults[numCastOps][numCastOps] = {
2164 // T F F U S F F P I B -+
2165 // R Z S P P I I T P 2 N T |
2166 // U E E 2 2 2 2 R E I T C +- secondOp
2167 // N X X U S F F N X N 2 V |
2168 // C T T I I P P C T T P T -+
2169 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2170 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2171 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002172 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2173 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002174 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2175 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2176 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2177 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2178 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2179 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2180 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2181 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002182
2183 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002184 // merging. However, bitcast of A->B->A are allowed.
2185 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2186 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2187 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2188
2189 // Check if any of the bitcasts convert scalars<->vectors.
2190 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2191 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2192 // Unless we are bitcasing to the original type, disallow optimizations.
2193 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002194
2195 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2196 [secondOp-Instruction::CastOpsBegin];
2197 switch (ElimCase) {
2198 case 0:
2199 // categorically disallowed
2200 return 0;
2201 case 1:
2202 // allowed, use first cast's opcode
2203 return firstOp;
2204 case 2:
2205 // allowed, use second cast's opcode
2206 return secondOp;
2207 case 3:
2208 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002209 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002210 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002211 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002212 return firstOp;
2213 return 0;
2214 case 4:
2215 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002216 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002217 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002218 return firstOp;
2219 return 0;
2220 case 5:
2221 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002222 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002223 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002224 return secondOp;
2225 return 0;
2226 case 6:
2227 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002228 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002229 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002230 return secondOp;
2231 return 0;
2232 case 7: {
2233 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002234 if (!IntPtrTy)
2235 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002236 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2237 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002238 if (MidSize >= PtrSize)
2239 return Instruction::BitCast;
2240 return 0;
2241 }
2242 case 8: {
2243 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2244 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2245 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002246 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2247 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248 if (SrcSize == DstSize)
2249 return Instruction::BitCast;
2250 else if (SrcSize < DstSize)
2251 return firstOp;
2252 return secondOp;
2253 }
2254 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2255 return Instruction::ZExt;
2256 case 10:
2257 // fpext followed by ftrunc is allowed if the bit size returned to is
2258 // the same as the original, in which case its just a bitcast
2259 if (SrcTy == DstTy)
2260 return Instruction::BitCast;
2261 return 0; // If the types are not the same we can't eliminate it.
2262 case 11:
2263 // bitcast followed by ptrtoint is allowed as long as the bitcast
2264 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002265 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002266 return secondOp;
2267 return 0;
2268 case 12:
2269 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002270 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271 return firstOp;
2272 return 0;
2273 case 13: {
2274 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002275 if (!IntPtrTy)
2276 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002277 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2278 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2279 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002280 if (SrcSize <= PtrSize && SrcSize == DstSize)
2281 return Instruction::BitCast;
2282 return 0;
2283 }
2284 case 99:
2285 // cast combination can't happen (error in input). This is for all cases
2286 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002287 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002288 default:
Craig Topperc514b542012-02-05 22:14:15 +00002289 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002290 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002291}
2292
Chris Lattner229907c2011-07-18 04:54:35 +00002293CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002294 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002295 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296 // Construct and return the appropriate CastInst subclass
2297 switch (op) {
2298 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2299 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2300 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2301 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2302 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2303 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2304 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2305 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2306 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2307 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2308 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2309 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topperc514b542012-02-05 22:14:15 +00002310 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312}
2313
Chris Lattner229907c2011-07-18 04:54:35 +00002314CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002315 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002316 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317 // Construct and return the appropriate CastInst subclass
2318 switch (op) {
2319 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2320 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2321 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2322 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2323 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2324 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2325 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2326 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2327 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2328 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2329 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2330 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topperc514b542012-02-05 22:14:15 +00002331 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002333}
2334
Chris Lattner229907c2011-07-18 04:54:35 +00002335CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002336 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002337 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002338 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002339 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2340 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002341}
2342
Chris Lattner229907c2011-07-18 04:54:35 +00002343CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002344 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002345 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002346 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002347 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2348 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002349}
2350
Chris Lattner229907c2011-07-18 04:54:35 +00002351CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002352 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002353 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002354 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002355 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2356 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002357}
2358
Chris Lattner229907c2011-07-18 04:54:35 +00002359CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002360 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002361 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002362 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002363 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2364 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002365}
2366
Chris Lattner229907c2011-07-18 04:54:35 +00002367CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002368 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002369 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002370 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002371 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2372 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002373}
2374
Chris Lattner229907c2011-07-18 04:54:35 +00002375CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002376 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002377 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002378 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002379 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2380 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002381}
2382
Chris Lattner229907c2011-07-18 04:54:35 +00002383CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002384 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002385 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002386 assert(S->getType()->isPointerTy() && "Invalid cast");
2387 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002388 "Invalid cast");
2389
Duncan Sands9dff9be2010-02-15 16:12:20 +00002390 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002391 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2392 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002393}
2394
2395/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002396CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002397 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002398 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002399 assert(S->getType()->isPointerTy() && "Invalid cast");
2400 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002401 "Invalid cast");
2402
Duncan Sands9dff9be2010-02-15 16:12:20 +00002403 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002404 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2405 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002406}
2407
Chris Lattner229907c2011-07-18 04:54:35 +00002408CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002409 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002410 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002411 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002412 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002413 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2414 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002415 Instruction::CastOps opcode =
2416 (SrcBits == DstBits ? Instruction::BitCast :
2417 (SrcBits > DstBits ? Instruction::Trunc :
2418 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002419 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002420}
2421
Chris Lattner229907c2011-07-18 04:54:35 +00002422CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002423 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002424 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002425 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002426 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002427 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2428 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002429 Instruction::CastOps opcode =
2430 (SrcBits == DstBits ? Instruction::BitCast :
2431 (SrcBits > DstBits ? Instruction::Trunc :
2432 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002433 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002434}
2435
Chris Lattner229907c2011-07-18 04:54:35 +00002436CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002437 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002438 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002439 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002440 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002441 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2442 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002443 Instruction::CastOps opcode =
2444 (SrcBits == DstBits ? Instruction::BitCast :
2445 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002446 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002447}
2448
Chris Lattner229907c2011-07-18 04:54:35 +00002449CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002450 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002451 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002452 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002453 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002454 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2455 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002456 Instruction::CastOps opcode =
2457 (SrcBits == DstBits ? Instruction::BitCast :
2458 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002459 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002460}
2461
Duncan Sands55e50902008-01-06 10:12:28 +00002462// Check whether it is valid to call getCastOpcode for these types.
2463// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002464bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002465 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2466 return false;
2467
2468 if (SrcTy == DestTy)
2469 return true;
2470
Chris Lattner229907c2011-07-18 04:54:35 +00002471 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2472 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002473 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2474 // An element by element cast. Valid if casting the elements is valid.
2475 SrcTy = SrcVecTy->getElementType();
2476 DestTy = DestVecTy->getElementType();
2477 }
2478
Duncan Sands55e50902008-01-06 10:12:28 +00002479 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002480 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2481 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002482
2483 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002484 if (DestTy->isIntegerTy()) { // Casting to integral
2485 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002486 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002487 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002488 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002489 } else if (SrcTy->isVectorTy()) { // Casting from vector
2490 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002491 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002492 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002493 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002494 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2495 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002496 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002497 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002498 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002499 } else if (SrcTy->isVectorTy()) { // Casting from vector
2500 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002501 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002502 return false;
2503 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002504 } else if (DestTy->isVectorTy()) { // Casting to vector
2505 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002506 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002507 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002508 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002509 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002510 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002511 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002512 return false;
2513 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002514 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002515 if (SrcTy->isVectorTy()) {
2516 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002517 } else {
2518 return false;
2519 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002520 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002521 return false;
2522 }
2523}
2524
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002525// Provide a way to get a "cast" where the cast opcode is inferred from the
2526// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002527// logic in the castIsValid function below. This axiom should hold:
2528// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2529// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002531// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002533CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002534 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2535 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536
Duncan Sands55e50902008-01-06 10:12:28 +00002537 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2538 "Only first class types are castable!");
2539
Duncan Sandsa8514532011-05-18 07:13:41 +00002540 if (SrcTy == DestTy)
2541 return BitCast;
2542
Chris Lattner229907c2011-07-18 04:54:35 +00002543 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2544 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002545 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2546 // An element by element cast. Find the appropriate opcode based on the
2547 // element types.
2548 SrcTy = SrcVecTy->getElementType();
2549 DestTy = DestVecTy->getElementType();
2550 }
2551
2552 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002553 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2554 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002555
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002557 if (DestTy->isIntegerTy()) { // Casting to integral
2558 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559 if (DestBits < SrcBits)
2560 return Trunc; // int -> smaller int
2561 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002562 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563 return SExt; // signed -> SEXT
2564 else
2565 return ZExt; // unsigned -> ZEXT
2566 } else {
2567 return BitCast; // Same size, No-op cast
2568 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002569 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002570 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571 return FPToSI; // FP -> sint
2572 else
2573 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002574 } else if (SrcTy->isVectorTy()) {
2575 assert(DestBits == SrcBits &&
2576 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577 return BitCast; // Same size, no-op cast
2578 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002579 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580 "Casting from a value that is not first-class type");
2581 return PtrToInt; // ptr -> int
2582 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002583 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2584 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002585 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586 return SIToFP; // sint -> FP
2587 else
2588 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002589 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002590 if (DestBits < SrcBits) {
2591 return FPTrunc; // FP -> smaller FP
2592 } else if (DestBits > SrcBits) {
2593 return FPExt; // FP -> larger FP
2594 } else {
2595 return BitCast; // same size, no-op cast
2596 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002597 } else if (SrcTy->isVectorTy()) {
2598 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002599 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002600 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002602 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002603 } else if (DestTy->isVectorTy()) {
2604 assert(DestBits == SrcBits &&
2605 "Illegal cast to vector (wrong type or size)");
2606 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002607 } else if (DestTy->isPointerTy()) {
2608 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002610 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002613 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002614 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002615 if (SrcTy->isVectorTy()) {
2616 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002617 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002618 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002619 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002621 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622}
2623
2624//===----------------------------------------------------------------------===//
2625// CastInst SubClass Constructors
2626//===----------------------------------------------------------------------===//
2627
2628/// Check that the construction parameters for a CastInst are correct. This
2629/// could be broken out into the separate constructors but it is useful to have
2630/// it in one place and to eliminate the redundant code for getting the sizes
2631/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002632bool
Chris Lattner229907c2011-07-18 04:54:35 +00002633CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634
2635 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002636 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002637 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2638 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002639 return false;
2640
2641 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002642 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2643 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002644
Duncan Sands7f646562011-05-18 09:21:57 +00002645 // If these are vector types, get the lengths of the vectors (using zero for
2646 // scalar types means that checking that vector lengths match also checks that
2647 // scalars are not being converted to vectors or vectors to scalars).
2648 unsigned SrcLength = SrcTy->isVectorTy() ?
2649 cast<VectorType>(SrcTy)->getNumElements() : 0;
2650 unsigned DstLength = DstTy->isVectorTy() ?
2651 cast<VectorType>(DstTy)->getNumElements() : 0;
2652
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653 // Switch on the opcode provided
2654 switch (op) {
2655 default: return false; // This is an input error
2656 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002657 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2658 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002660 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2661 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002662 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002663 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2664 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002666 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2667 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002668 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002669 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2670 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002672 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002673 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2674 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002675 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002676 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002677 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2678 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002679 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002680 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002681 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002682 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2683 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2684 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002685 return SrcTy->getScalarType()->isPointerTy() &&
2686 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002687 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002688 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002689 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002690 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2691 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2692 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002693 return SrcTy->getScalarType()->isIntegerTy() &&
2694 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002695 case Instruction::BitCast:
2696 // BitCast implies a no-op cast of type only. No bits change.
2697 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002698 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002699 return false;
2700
Duncan Sands55e50902008-01-06 10:12:28 +00002701 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002702 // these cases, the cast is okay if the source and destination bit widths
2703 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002704 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002705 }
2706}
2707
2708TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002709 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002710) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002711 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002712}
2713
2714TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002715 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002716) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002717 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002718}
2719
2720ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002721 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002722) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002723 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002724}
2725
2726ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002727 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002728) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002729 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002730}
2731SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002732 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002733) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002734 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002735}
2736
Jeff Cohencc08c832006-12-02 02:22:01 +00002737SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002738 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002740 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002741}
2742
2743FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002744 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002745) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002746 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002747}
2748
2749FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002750 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002751) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002752 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002753}
2754
2755FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002756 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002757) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002758 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002759}
2760
2761FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002762 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002763) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002764 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002765}
2766
2767UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002768 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002769) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002770 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002771}
2772
2773UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002774 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002775) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002776 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002777}
2778
2779SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002780 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002781) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002782 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002783}
2784
2785SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002786 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002787) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002788 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002789}
2790
2791FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002792 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002793) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002794 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002795}
2796
2797FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002798 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002799) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002800 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002801}
2802
2803FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002804 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002805) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002806 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002807}
2808
2809FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002810 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002811) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002812 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002813}
2814
2815PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002816 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002817) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002818 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002819}
2820
2821PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002822 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002823) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002824 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002825}
2826
2827IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002828 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002829) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002830 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002831}
2832
2833IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002834 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002835) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002836 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002837}
2838
2839BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002840 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002841) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002842 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002843}
2844
2845BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002846 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002847) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002848 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002849}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002850
2851//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002852// CmpInst Classes
2853//===----------------------------------------------------------------------===//
2854
Craig Topper3186c012012-09-23 02:12:10 +00002855void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002856
Chris Lattner229907c2011-07-18 04:54:35 +00002857CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002858 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002859 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002860 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002861 OperandTraits<CmpInst>::op_begin(this),
2862 OperandTraits<CmpInst>::operands(this),
2863 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002864 Op<0>() = LHS;
2865 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002866 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002867 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002868}
Gabor Greiff6caff662008-05-10 08:32:32 +00002869
Chris Lattner229907c2011-07-18 04:54:35 +00002870CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002871 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002872 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002873 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002874 OperandTraits<CmpInst>::op_begin(this),
2875 OperandTraits<CmpInst>::operands(this),
2876 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002877 Op<0>() = LHS;
2878 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002879 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002880 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002881}
2882
2883CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002884CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002885 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002886 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002887 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002888 if (InsertBefore)
2889 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2890 S1, S2, Name);
2891 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002892 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002893 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002894 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002895
2896 if (InsertBefore)
2897 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2898 S1, S2, Name);
2899 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002900 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002901 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002902}
2903
2904CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002905CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002906 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002907 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002908 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2909 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002910 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002911 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2912 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002913}
2914
2915void CmpInst::swapOperands() {
2916 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2917 IC->swapOperands();
2918 else
2919 cast<FCmpInst>(this)->swapOperands();
2920}
2921
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002922bool CmpInst::isCommutative() const {
2923 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002924 return IC->isCommutative();
2925 return cast<FCmpInst>(this)->isCommutative();
2926}
2927
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002928bool CmpInst::isEquality() const {
2929 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002930 return IC->isEquality();
2931 return cast<FCmpInst>(this)->isEquality();
2932}
2933
2934
Dan Gohman4e724382008-05-31 02:47:54 +00002935CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002936 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002937 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002938 case ICMP_EQ: return ICMP_NE;
2939 case ICMP_NE: return ICMP_EQ;
2940 case ICMP_UGT: return ICMP_ULE;
2941 case ICMP_ULT: return ICMP_UGE;
2942 case ICMP_UGE: return ICMP_ULT;
2943 case ICMP_ULE: return ICMP_UGT;
2944 case ICMP_SGT: return ICMP_SLE;
2945 case ICMP_SLT: return ICMP_SGE;
2946 case ICMP_SGE: return ICMP_SLT;
2947 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002948
Dan Gohman4e724382008-05-31 02:47:54 +00002949 case FCMP_OEQ: return FCMP_UNE;
2950 case FCMP_ONE: return FCMP_UEQ;
2951 case FCMP_OGT: return FCMP_ULE;
2952 case FCMP_OLT: return FCMP_UGE;
2953 case FCMP_OGE: return FCMP_ULT;
2954 case FCMP_OLE: return FCMP_UGT;
2955 case FCMP_UEQ: return FCMP_ONE;
2956 case FCMP_UNE: return FCMP_OEQ;
2957 case FCMP_UGT: return FCMP_OLE;
2958 case FCMP_ULT: return FCMP_OGE;
2959 case FCMP_UGE: return FCMP_OLT;
2960 case FCMP_ULE: return FCMP_OGT;
2961 case FCMP_ORD: return FCMP_UNO;
2962 case FCMP_UNO: return FCMP_ORD;
2963 case FCMP_TRUE: return FCMP_FALSE;
2964 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002965 }
2966}
2967
Reid Spencer266e42b2006-12-23 06:05:41 +00002968ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2969 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002970 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002971 case ICMP_EQ: case ICMP_NE:
2972 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2973 return pred;
2974 case ICMP_UGT: return ICMP_SGT;
2975 case ICMP_ULT: return ICMP_SLT;
2976 case ICMP_UGE: return ICMP_SGE;
2977 case ICMP_ULE: return ICMP_SLE;
2978 }
2979}
2980
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002981ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2982 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002983 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002984 case ICMP_EQ: case ICMP_NE:
2985 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2986 return pred;
2987 case ICMP_SGT: return ICMP_UGT;
2988 case ICMP_SLT: return ICMP_ULT;
2989 case ICMP_SGE: return ICMP_UGE;
2990 case ICMP_SLE: return ICMP_ULE;
2991 }
2992}
2993
Reid Spencer0286bc12007-02-28 22:00:54 +00002994/// Initialize a set of values that all satisfy the condition with C.
2995///
2996ConstantRange
2997ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2998 APInt Lower(C);
2999 APInt Upper(C);
3000 uint32_t BitWidth = C.getBitWidth();
3001 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003002 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00003003 case ICmpInst::ICMP_EQ: Upper++; break;
3004 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003005 case ICmpInst::ICMP_ULT:
3006 Lower = APInt::getMinValue(BitWidth);
3007 // Check for an empty-set condition.
3008 if (Lower == Upper)
3009 return ConstantRange(BitWidth, /*isFullSet=*/false);
3010 break;
3011 case ICmpInst::ICMP_SLT:
3012 Lower = APInt::getSignedMinValue(BitWidth);
3013 // Check for an empty-set condition.
3014 if (Lower == Upper)
3015 return ConstantRange(BitWidth, /*isFullSet=*/false);
3016 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003017 case ICmpInst::ICMP_UGT:
3018 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003019 // Check for an empty-set condition.
3020 if (Lower == Upper)
3021 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003022 break;
3023 case ICmpInst::ICMP_SGT:
3024 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003025 // Check for an empty-set condition.
3026 if (Lower == Upper)
3027 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003028 break;
3029 case ICmpInst::ICMP_ULE:
3030 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003031 // Check for a full-set condition.
3032 if (Lower == Upper)
3033 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003034 break;
3035 case ICmpInst::ICMP_SLE:
3036 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003037 // Check for a full-set condition.
3038 if (Lower == Upper)
3039 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003040 break;
3041 case ICmpInst::ICMP_UGE:
3042 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003043 // Check for a full-set condition.
3044 if (Lower == Upper)
3045 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003046 break;
3047 case ICmpInst::ICMP_SGE:
3048 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003049 // Check for a full-set condition.
3050 if (Lower == Upper)
3051 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003052 break;
3053 }
3054 return ConstantRange(Lower, Upper);
3055}
3056
Dan Gohman4e724382008-05-31 02:47:54 +00003057CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003058 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003059 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003060 case ICMP_EQ: case ICMP_NE:
3061 return pred;
3062 case ICMP_SGT: return ICMP_SLT;
3063 case ICMP_SLT: return ICMP_SGT;
3064 case ICMP_SGE: return ICMP_SLE;
3065 case ICMP_SLE: return ICMP_SGE;
3066 case ICMP_UGT: return ICMP_ULT;
3067 case ICMP_ULT: return ICMP_UGT;
3068 case ICMP_UGE: return ICMP_ULE;
3069 case ICMP_ULE: return ICMP_UGE;
3070
Reid Spencerd9436b62006-11-20 01:22:35 +00003071 case FCMP_FALSE: case FCMP_TRUE:
3072 case FCMP_OEQ: case FCMP_ONE:
3073 case FCMP_UEQ: case FCMP_UNE:
3074 case FCMP_ORD: case FCMP_UNO:
3075 return pred;
3076 case FCMP_OGT: return FCMP_OLT;
3077 case FCMP_OLT: return FCMP_OGT;
3078 case FCMP_OGE: return FCMP_OLE;
3079 case FCMP_OLE: return FCMP_OGE;
3080 case FCMP_UGT: return FCMP_ULT;
3081 case FCMP_ULT: return FCMP_UGT;
3082 case FCMP_UGE: return FCMP_ULE;
3083 case FCMP_ULE: return FCMP_UGE;
3084 }
3085}
3086
Reid Spencer266e42b2006-12-23 06:05:41 +00003087bool CmpInst::isUnsigned(unsigned short predicate) {
3088 switch (predicate) {
3089 default: return false;
3090 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3091 case ICmpInst::ICMP_UGE: return true;
3092 }
3093}
3094
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003095bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003096 switch (predicate) {
3097 default: return false;
3098 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3099 case ICmpInst::ICMP_SGE: return true;
3100 }
3101}
3102
3103bool CmpInst::isOrdered(unsigned short predicate) {
3104 switch (predicate) {
3105 default: return false;
3106 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3107 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3108 case FCmpInst::FCMP_ORD: return true;
3109 }
3110}
3111
3112bool CmpInst::isUnordered(unsigned short predicate) {
3113 switch (predicate) {
3114 default: return false;
3115 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3116 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3117 case FCmpInst::FCMP_UNO: return true;
3118 }
3119}
3120
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003121bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3122 switch(predicate) {
3123 default: return false;
3124 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3125 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3126 }
3127}
3128
3129bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3130 switch(predicate) {
3131 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3132 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3133 default: return false;
3134 }
3135}
3136
3137
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003138//===----------------------------------------------------------------------===//
3139// SwitchInst Implementation
3140//===----------------------------------------------------------------------===//
3141
Chris Lattnerbaf00152010-11-17 05:41:46 +00003142void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3143 assert(Value && Default && NumReserved);
3144 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003145 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003146 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003147
Gabor Greif2d3024d2008-05-26 21:33:52 +00003148 OperandList[0] = Value;
3149 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003150}
3151
Chris Lattner2195fc42007-02-24 00:55:48 +00003152/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3153/// switch on and a default destination. The number of additional cases can
3154/// be specified here to make memory allocation more efficient. This
3155/// constructor can also autoinsert before another instruction.
3156SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3157 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003158 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3159 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003160 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003161}
3162
3163/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3164/// switch on and a default destination. The number of additional cases can
3165/// be specified here to make memory allocation more efficient. This
3166/// constructor also autoinserts at the end of the specified BasicBlock.
3167SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3168 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003169 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3170 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003171 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003172}
3173
Misha Brukmanb1c93172005-04-21 23:48:37 +00003174SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003175 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3176 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3177 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003178 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003179 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003180 OL[i] = InOL[i];
3181 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003182 }
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003183 TheSubsets = SI.TheSubsets;
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003184 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003185}
3186
Gordon Henriksen14a55692007-12-10 02:14:30 +00003187SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003188 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003189}
3190
3191
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003192/// addCase - Add an entry to the switch instruction...
3193///
Chris Lattner47ac1872005-02-24 05:32:09 +00003194void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003195 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00003196
3197 // FIXME: Currently we work with ConstantInt based cases.
3198 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003199 Mapping.add(IntItem::fromConstantInt(OnVal));
3200 IntegersSubset CaseRanges = Mapping.getCase();
3201 addCase(CaseRanges, Dest);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00003202}
3203
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003204void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003205 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003206 unsigned OpNo = NumOperands;
3207 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003208 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003209 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003210 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003211 NumOperands = OpNo+2;
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003212
3213 SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3214
3215 CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3216 Case.updateCaseValueOperand(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003217 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003218}
3219
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003220/// removeCase - This method removes the specified case and its successor
3221/// from the switch instruction.
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003222void SwitchInst::removeCase(CaseIt& i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003223 unsigned idx = i.getCaseIndex();
3224
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003225 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003226
3227 unsigned NumOps = getNumOperands();
3228 Use *OL = OperandList;
3229
Jay Foad14277722011-02-01 09:22:34 +00003230 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003231 if (2 + (idx + 1) * 2 != NumOps) {
3232 OL[2 + idx * 2] = OL[NumOps - 2];
3233 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003234 }
3235
3236 // Nuke the last value.
3237 OL[NumOps-2].set(0);
3238 OL[NumOps-2+1].set(0);
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003239
3240 // Do the same with TheCases collection:
3241 if (i.SubsetIt != --TheSubsets.end()) {
3242 *i.SubsetIt = TheSubsets.back();
3243 TheSubsets.pop_back();
3244 } else {
3245 TheSubsets.pop_back();
3246 i.SubsetIt = TheSubsets.end();
3247 }
3248
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003249 NumOperands = NumOps-2;
3250}
3251
Jay Foade98f29d2011-04-01 08:00:58 +00003252/// growOperands - grow operands - This grows the operand list in response
3253/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003254///
Jay Foade98f29d2011-04-01 08:00:58 +00003255void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003256 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003257 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003258
3259 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003260 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003261 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003262 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003263 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003264 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003265 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003266 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003267}
3268
3269
3270BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3271 return getSuccessor(idx);
3272}
3273unsigned SwitchInst::getNumSuccessorsV() const {
3274 return getNumSuccessors();
3275}
3276void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3277 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003278}
Chris Lattnerf22be932004-10-15 23:52:53 +00003279
Chris Lattner3ed871f2009-10-27 19:13:16 +00003280//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003281// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003282//===----------------------------------------------------------------------===//
3283
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003284void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003285 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003286 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003287 ReservedSpace = 1+NumDests;
3288 NumOperands = 1;
3289 OperandList = allocHungoffUses(ReservedSpace);
3290
3291 OperandList[0] = Address;
3292}
3293
3294
Jay Foade98f29d2011-04-01 08:00:58 +00003295/// growOperands - grow operands - This grows the operand list in response
3296/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003297///
Jay Foade98f29d2011-04-01 08:00:58 +00003298void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003299 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003300 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003301
3302 ReservedSpace = NumOps;
3303 Use *NewOps = allocHungoffUses(NumOps);
3304 Use *OldOps = OperandList;
3305 for (unsigned i = 0; i != e; ++i)
3306 NewOps[i] = OldOps[i];
3307 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003308 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003309}
3310
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003311IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3312 Instruction *InsertBefore)
3313: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003314 0, 0, InsertBefore) {
3315 init(Address, NumCases);
3316}
3317
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003318IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3319 BasicBlock *InsertAtEnd)
3320: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003321 0, 0, InsertAtEnd) {
3322 init(Address, NumCases);
3323}
3324
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003325IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3326 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003327 allocHungoffUses(IBI.getNumOperands()),
3328 IBI.getNumOperands()) {
3329 Use *OL = OperandList, *InOL = IBI.OperandList;
3330 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3331 OL[i] = InOL[i];
3332 SubclassOptionalData = IBI.SubclassOptionalData;
3333}
3334
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003335IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003336 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003337}
3338
3339/// addDestination - Add a destination.
3340///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003341void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003342 unsigned OpNo = NumOperands;
3343 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003344 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003345 // Initialize some new operands.
3346 assert(OpNo < ReservedSpace && "Growing didn't work!");
3347 NumOperands = OpNo+1;
3348 OperandList[OpNo] = DestBB;
3349}
3350
3351/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003352/// indirectbr instruction.
3353void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003354 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3355
3356 unsigned NumOps = getNumOperands();
3357 Use *OL = OperandList;
3358
3359 // Replace this value with the last one.
3360 OL[idx+1] = OL[NumOps-1];
3361
3362 // Nuke the last value.
3363 OL[NumOps-1].set(0);
3364 NumOperands = NumOps-1;
3365}
3366
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003367BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003368 return getSuccessor(idx);
3369}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003370unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003371 return getNumSuccessors();
3372}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003373void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003374 setSuccessor(idx, B);
3375}
3376
3377//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003378// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003379//===----------------------------------------------------------------------===//
3380
Chris Lattnerf22be932004-10-15 23:52:53 +00003381// Define these methods here so vtables don't get emitted into every translation
3382// unit that uses these classes.
3383
Devang Patel11cf3f42009-10-27 22:16:29 +00003384GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3385 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003386}
3387
Devang Patel11cf3f42009-10-27 22:16:29 +00003388BinaryOperator *BinaryOperator::clone_impl() const {
3389 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003390}
3391
Devang Patel11cf3f42009-10-27 22:16:29 +00003392FCmpInst* FCmpInst::clone_impl() const {
3393 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003394}
3395
Devang Patel11cf3f42009-10-27 22:16:29 +00003396ICmpInst* ICmpInst::clone_impl() const {
3397 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003398}
3399
Devang Patel11cf3f42009-10-27 22:16:29 +00003400ExtractValueInst *ExtractValueInst::clone_impl() const {
3401 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003402}
3403
Devang Patel11cf3f42009-10-27 22:16:29 +00003404InsertValueInst *InsertValueInst::clone_impl() const {
3405 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003406}
3407
Devang Patel11cf3f42009-10-27 22:16:29 +00003408AllocaInst *AllocaInst::clone_impl() const {
3409 return new AllocaInst(getAllocatedType(),
3410 (Value*)getOperand(0),
3411 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003412}
3413
Devang Patel11cf3f42009-10-27 22:16:29 +00003414LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003415 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3416 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003417}
3418
Devang Patel11cf3f42009-10-27 22:16:29 +00003419StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003420 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003421 getAlignment(), getOrdering(), getSynchScope());
3422
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003423}
3424
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003425AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3426 AtomicCmpXchgInst *Result =
3427 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3428 getOrdering(), getSynchScope());
3429 Result->setVolatile(isVolatile());
3430 return Result;
3431}
3432
3433AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3434 AtomicRMWInst *Result =
3435 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3436 getOrdering(), getSynchScope());
3437 Result->setVolatile(isVolatile());
3438 return Result;
3439}
3440
Eli Friedmanfee02c62011-07-25 23:16:38 +00003441FenceInst *FenceInst::clone_impl() const {
3442 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3443}
3444
Devang Patel11cf3f42009-10-27 22:16:29 +00003445TruncInst *TruncInst::clone_impl() const {
3446 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003447}
3448
Devang Patel11cf3f42009-10-27 22:16:29 +00003449ZExtInst *ZExtInst::clone_impl() const {
3450 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003451}
3452
Devang Patel11cf3f42009-10-27 22:16:29 +00003453SExtInst *SExtInst::clone_impl() const {
3454 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003455}
3456
Devang Patel11cf3f42009-10-27 22:16:29 +00003457FPTruncInst *FPTruncInst::clone_impl() const {
3458 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003459}
3460
Devang Patel11cf3f42009-10-27 22:16:29 +00003461FPExtInst *FPExtInst::clone_impl() const {
3462 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003463}
3464
Devang Patel11cf3f42009-10-27 22:16:29 +00003465UIToFPInst *UIToFPInst::clone_impl() const {
3466 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003467}
3468
Devang Patel11cf3f42009-10-27 22:16:29 +00003469SIToFPInst *SIToFPInst::clone_impl() const {
3470 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003471}
3472
Devang Patel11cf3f42009-10-27 22:16:29 +00003473FPToUIInst *FPToUIInst::clone_impl() const {
3474 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003475}
3476
Devang Patel11cf3f42009-10-27 22:16:29 +00003477FPToSIInst *FPToSIInst::clone_impl() const {
3478 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003479}
3480
Devang Patel11cf3f42009-10-27 22:16:29 +00003481PtrToIntInst *PtrToIntInst::clone_impl() const {
3482 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003483}
3484
Devang Patel11cf3f42009-10-27 22:16:29 +00003485IntToPtrInst *IntToPtrInst::clone_impl() const {
3486 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003487}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003488
Devang Patel11cf3f42009-10-27 22:16:29 +00003489BitCastInst *BitCastInst::clone_impl() const {
3490 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003491}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003492
Devang Patel11cf3f42009-10-27 22:16:29 +00003493CallInst *CallInst::clone_impl() const {
3494 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003495}
3496
Devang Patel11cf3f42009-10-27 22:16:29 +00003497SelectInst *SelectInst::clone_impl() const {
3498 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003499}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003500
Devang Patel11cf3f42009-10-27 22:16:29 +00003501VAArgInst *VAArgInst::clone_impl() const {
3502 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003503}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003504
Devang Patel11cf3f42009-10-27 22:16:29 +00003505ExtractElementInst *ExtractElementInst::clone_impl() const {
3506 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003507}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003508
Devang Patel11cf3f42009-10-27 22:16:29 +00003509InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003510 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003511}
3512
Devang Patel11cf3f42009-10-27 22:16:29 +00003513ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003514 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003515}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003516
Devang Patel11cf3f42009-10-27 22:16:29 +00003517PHINode *PHINode::clone_impl() const {
3518 return new PHINode(*this);
3519}
3520
Bill Wendlingfae14752011-08-12 20:24:12 +00003521LandingPadInst *LandingPadInst::clone_impl() const {
3522 return new LandingPadInst(*this);
3523}
3524
Devang Patel11cf3f42009-10-27 22:16:29 +00003525ReturnInst *ReturnInst::clone_impl() const {
3526 return new(getNumOperands()) ReturnInst(*this);
3527}
3528
3529BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003530 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003531}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003532
Devang Patel11cf3f42009-10-27 22:16:29 +00003533SwitchInst *SwitchInst::clone_impl() const {
3534 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003535}
3536
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003537IndirectBrInst *IndirectBrInst::clone_impl() const {
3538 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003539}
3540
3541
Devang Patel11cf3f42009-10-27 22:16:29 +00003542InvokeInst *InvokeInst::clone_impl() const {
3543 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003544}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003545
Bill Wendlingf891bf82011-07-31 06:30:59 +00003546ResumeInst *ResumeInst::clone_impl() const {
3547 return new(1) ResumeInst(*this);
3548}
3549
Devang Patel11cf3f42009-10-27 22:16:29 +00003550UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003551 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003552 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003553}