blob: 2f6c069a2d368d4b2b5daeb4dfdfa193331918fe [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)
164 if (getIncomingValue(i) != ConstantValue)
165 return 0; // Incoming values not all the same.
166 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000167}
168
Bill Wendlingfae14752011-08-12 20:24:12 +0000169//===----------------------------------------------------------------------===//
170// LandingPadInst Implementation
171//===----------------------------------------------------------------------===//
172
173LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
174 unsigned NumReservedValues, const Twine &NameStr,
175 Instruction *InsertBefore)
176 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) {
177 init(PersonalityFn, 1 + NumReservedValues, NameStr);
178}
179
180LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
181 unsigned NumReservedValues, const Twine &NameStr,
182 BasicBlock *InsertAtEnd)
183 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) {
184 init(PersonalityFn, 1 + NumReservedValues, NameStr);
185}
186
187LandingPadInst::LandingPadInst(const LandingPadInst &LP)
188 : Instruction(LP.getType(), Instruction::LandingPad,
189 allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()),
190 ReservedSpace(LP.getNumOperands()) {
191 Use *OL = OperandList, *InOL = LP.OperandList;
192 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
193 OL[I] = InOL[I];
194
195 setCleanup(LP.isCleanup());
196}
197
198LandingPadInst::~LandingPadInst() {
199 dropHungoffUses();
200}
201
202LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
203 unsigned NumReservedClauses,
204 const Twine &NameStr,
205 Instruction *InsertBefore) {
206 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
207 InsertBefore);
208}
209
210LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
211 unsigned NumReservedClauses,
212 const Twine &NameStr,
213 BasicBlock *InsertAtEnd) {
214 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
215 InsertAtEnd);
216}
217
218void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
219 const Twine &NameStr) {
220 ReservedSpace = NumReservedValues;
221 NumOperands = 1;
222 OperandList = allocHungoffUses(ReservedSpace);
223 OperandList[0] = PersFn;
224 setName(NameStr);
225 setCleanup(false);
226}
227
228/// growOperands - grow operands - This grows the operand list in response to a
229/// push_back style of operation. This grows the number of ops by 2 times.
230void LandingPadInst::growOperands(unsigned Size) {
231 unsigned e = getNumOperands();
232 if (ReservedSpace >= e + Size) return;
233 ReservedSpace = (e + Size / 2) * 2;
234
235 Use *NewOps = allocHungoffUses(ReservedSpace);
236 Use *OldOps = OperandList;
237 for (unsigned i = 0; i != e; ++i)
238 NewOps[i] = OldOps[i];
239
240 OperandList = NewOps;
241 Use::zap(OldOps, OldOps + e, true);
242}
243
244void LandingPadInst::addClause(Value *Val) {
245 unsigned OpNo = getNumOperands();
246 growOperands(1);
247 assert(OpNo < ReservedSpace && "Growing didn't work!");
248 ++NumOperands;
249 OperandList[OpNo] = Val;
250}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000251
252//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253// CallInst Implementation
254//===----------------------------------------------------------------------===//
255
Gordon Henriksen14a55692007-12-10 02:14:30 +0000256CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000257}
258
Jay Foad5bd375a2011-07-15 08:37:34 +0000259void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
260 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000261 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262
Jay Foad5bd375a2011-07-15 08:37:34 +0000263#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000264 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
266
Jay Foad5bd375a2011-07-15 08:37:34 +0000267 assert((Args.size() == FTy->getNumParams() ||
268 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000269 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000270
271 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000272 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000273 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000274 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000275#endif
276
277 std::copy(Args.begin(), Args.end(), op_begin());
278 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279}
280
Jay Foad5bd375a2011-07-15 08:37:34 +0000281void CallInst::init(Value *Func, const Twine &NameStr) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000282 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000283 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000284
Jay Foad5bd375a2011-07-15 08:37:34 +0000285#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000286 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
288
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000289 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000290#endif
291
292 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000293}
294
Daniel Dunbar4975db62009-07-25 04:41:11 +0000295CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296 Instruction *InsertBefore)
297 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
298 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000299 Instruction::Call,
300 OperandTraits<CallInst>::op_end(this) - 1,
301 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000302 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303}
304
Daniel Dunbar4975db62009-07-25 04:41:11 +0000305CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306 BasicBlock *InsertAtEnd)
307 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
308 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000309 Instruction::Call,
310 OperandTraits<CallInst>::op_end(this) - 1,
311 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000312 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313}
314
Misha Brukmanb1c93172005-04-21 23:48:37 +0000315CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000316 : Instruction(CI.getType(), Instruction::Call,
317 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000318 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000319 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000320 setTailCall(CI.isTailCall());
321 setCallingConv(CI.getCallingConv());
322
Jay Foad5bd375a2011-07-15 08:37:34 +0000323 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000324 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325}
326
Devang Patel4c758ea2008-09-25 21:00:45 +0000327void CallInst::addAttribute(unsigned i, Attributes attr) {
328 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000329 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000330 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000331}
332
Devang Patel4c758ea2008-09-25 21:00:45 +0000333void CallInst::removeAttribute(unsigned i, Attributes attr) {
334 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000335 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000336 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000337}
338
Devang Patelba3fa6c2008-09-23 23:03:40 +0000339bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000340 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000341 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000342 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000343 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000344 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000345}
346
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000347/// IsConstantOne - Return true only if val is constant int 1
348static bool IsConstantOne(Value *val) {
349 assert(val && "IsConstantOne does not work with NULL val");
350 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
351}
352
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000353static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000354 BasicBlock *InsertAtEnd, Type *IntPtrTy,
355 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000356 Value *ArraySize, Function *MallocF,
357 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000358 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000359 "createMalloc needs either InsertBefore or InsertAtEnd");
360
361 // malloc(type) becomes:
362 // bitcast (i8* malloc(typeSize)) to type*
363 // malloc(type, arraySize) becomes:
364 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000365 if (!ArraySize)
366 ArraySize = ConstantInt::get(IntPtrTy, 1);
367 else if (ArraySize->getType() != IntPtrTy) {
368 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000369 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
370 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000371 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000372 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
373 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000374 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000375
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000376 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000377 if (IsConstantOne(AllocSize)) {
378 AllocSize = ArraySize; // Operand * 1 = Operand
379 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
380 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
381 false /*ZExt*/);
382 // Malloc arg is constant product of type size and array size
383 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
384 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000385 // Multiply type size by the array size...
386 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000387 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
388 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000389 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000390 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
391 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000392 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000393 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000394
Victor Hernandez788eaab2009-09-18 19:20:02 +0000395 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000396 // Create the call to Malloc.
397 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
398 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000399 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000400 Value *MallocFunc = MallocF;
401 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000402 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000403 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000404 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000405 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000406 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000407 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000408 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000409 Result = MCall;
410 if (Result->getType() != AllocPtrType)
411 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000412 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000413 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000414 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000415 Result = MCall;
416 if (Result->getType() != AllocPtrType) {
417 InsertAtEnd->getInstList().push_back(MCall);
418 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000419 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000420 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000421 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000422 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000423 if (Function *F = dyn_cast<Function>(MallocFunc)) {
424 MCall->setCallingConv(F->getCallingConv());
425 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
426 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000427 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000428
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000429 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000430}
431
432/// CreateMalloc - Generate the IR for a call to malloc:
433/// 1. Compute the malloc call's argument as the specified type's size,
434/// possibly multiplied by the array size if the array size is not
435/// constant 1.
436/// 2. Call malloc with that argument.
437/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000438Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000439 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000440 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000441 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000442 const Twine &Name) {
443 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000444 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445}
446
447/// CreateMalloc - Generate the IR for a call to malloc:
448/// 1. Compute the malloc call's argument as the specified type's size,
449/// possibly multiplied by the array size if the array size is not
450/// constant 1.
451/// 2. Call malloc with that argument.
452/// 3. Bitcast the result of the malloc call to the specified type.
453/// Note: This function does not add the bitcast to the basic block, that is the
454/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000455Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000456 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000457 Value *AllocSize, Value *ArraySize,
458 Function *MallocF, const Twine &Name) {
459 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000460 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000461}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000462
Victor Hernandeze2971492009-10-24 04:23:03 +0000463static Instruction* createFree(Value* Source, Instruction *InsertBefore,
464 BasicBlock *InsertAtEnd) {
465 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
466 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000467 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000468 "Can not free something of nonpointer type!");
469
470 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
471 Module* M = BB->getParent()->getParent();
472
Chris Lattner229907c2011-07-18 04:54:35 +0000473 Type *VoidTy = Type::getVoidTy(M->getContext());
474 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000475 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000476 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000477 CallInst* Result = NULL;
478 Value *PtrCast = Source;
479 if (InsertBefore) {
480 if (Source->getType() != IntPtrTy)
481 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
482 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
483 } else {
484 if (Source->getType() != IntPtrTy)
485 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
486 Result = CallInst::Create(FreeFunc, PtrCast, "");
487 }
488 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000489 if (Function *F = dyn_cast<Function>(FreeFunc))
490 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000491
492 return Result;
493}
494
495/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000496Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
497 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000498}
499
500/// CreateFree - Generate the IR for a call to the builtin free function.
501/// Note: This function does not add the call to the basic block, that is the
502/// responsibility of the caller.
503Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
504 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
505 assert(FreeCall && "CreateFree did not create a CallInst");
506 return FreeCall;
507}
508
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000509//===----------------------------------------------------------------------===//
510// InvokeInst Implementation
511//===----------------------------------------------------------------------===//
512
513void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000514 ArrayRef<Value *> Args, const Twine &NameStr) {
515 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000516 Op<-3>() = Fn;
517 Op<-2>() = IfNormal;
518 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000519
520#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000521 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000522 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000523
Jay Foad5bd375a2011-07-15 08:37:34 +0000524 assert(((Args.size() == FTy->getNumParams()) ||
525 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000526 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000527
Jay Foad5bd375a2011-07-15 08:37:34 +0000528 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000529 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000530 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000531 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000532#endif
533
534 std::copy(Args.begin(), Args.end(), op_begin());
535 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000536}
537
Misha Brukmanb1c93172005-04-21 23:48:37 +0000538InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000539 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000540 OperandTraits<InvokeInst>::op_end(this)
541 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000542 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000543 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000544 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000545 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000546 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000547}
548
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000549BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
550 return getSuccessor(idx);
551}
552unsigned InvokeInst::getNumSuccessorsV() const {
553 return getNumSuccessors();
554}
555void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
556 return setSuccessor(idx, B);
557}
558
Devang Patelba3fa6c2008-09-23 23:03:40 +0000559bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000560 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000561 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000562 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000563 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000564 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000565}
566
Devang Patel4c758ea2008-09-25 21:00:45 +0000567void InvokeInst::addAttribute(unsigned i, Attributes attr) {
568 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000569 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000570 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000571}
572
Devang Patel4c758ea2008-09-25 21:00:45 +0000573void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
574 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000575 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000576 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000577}
578
Bill Wendlingfae14752011-08-12 20:24:12 +0000579LandingPadInst *InvokeInst::getLandingPadInst() const {
580 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
581}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000582
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000583//===----------------------------------------------------------------------===//
584// ReturnInst Implementation
585//===----------------------------------------------------------------------===//
586
Chris Lattner2195fc42007-02-24 00:55:48 +0000587ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000588 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000589 OperandTraits<ReturnInst>::op_end(this) -
590 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000591 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000592 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000593 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000594 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000595}
596
Owen Anderson55f1c092009-08-13 21:58:54 +0000597ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
598 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000599 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
600 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000601 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000602 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000603}
Owen Anderson55f1c092009-08-13 21:58:54 +0000604ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
605 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000606 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
607 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000608 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000609 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000610}
Owen Anderson55f1c092009-08-13 21:58:54 +0000611ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
612 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000613 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000614}
615
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000616unsigned ReturnInst::getNumSuccessorsV() const {
617 return getNumSuccessors();
618}
619
Devang Patelae682fb2008-02-26 17:56:20 +0000620/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
621/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000622void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000623 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000624}
625
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000626BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000627 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628 return 0;
629}
630
Devang Patel59643e52008-02-23 00:35:18 +0000631ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000632}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000633
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000634//===----------------------------------------------------------------------===//
635// UnwindInst Implementation
636//===----------------------------------------------------------------------===//
637
Owen Anderson55f1c092009-08-13 21:58:54 +0000638UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
639 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
640 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000641}
Owen Anderson55f1c092009-08-13 21:58:54 +0000642UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
643 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
644 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000645}
646
647
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648unsigned UnwindInst::getNumSuccessorsV() const {
649 return getNumSuccessors();
650}
651
652void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000653 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000654}
655
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000657 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658 return 0;
659}
660
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000661//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000662// ResumeInst Implementation
663//===----------------------------------------------------------------------===//
664
665ResumeInst::ResumeInst(const ResumeInst &RI)
666 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
667 OperandTraits<ResumeInst>::op_begin(this), 1) {
668 Op<0>() = RI.Op<0>();
669}
670
671ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
672 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
673 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
674 Op<0>() = Exn;
675}
676
677ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
678 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
679 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
680 Op<0>() = Exn;
681}
682
683unsigned ResumeInst::getNumSuccessorsV() const {
684 return getNumSuccessors();
685}
686
687void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
688 llvm_unreachable("ResumeInst has no successors!");
689}
690
691BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
692 llvm_unreachable("ResumeInst has no successors!");
693 return 0;
694}
695
696//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000697// UnreachableInst Implementation
698//===----------------------------------------------------------------------===//
699
Owen Anderson55f1c092009-08-13 21:58:54 +0000700UnreachableInst::UnreachableInst(LLVMContext &Context,
701 Instruction *InsertBefore)
702 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
703 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
Owen Anderson55f1c092009-08-13 21:58:54 +0000705UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
706 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
707 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000708}
709
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000710unsigned UnreachableInst::getNumSuccessorsV() const {
711 return getNumSuccessors();
712}
713
714void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendlingad088e62011-07-30 05:42:50 +0000715 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000716}
717
718BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendlingad088e62011-07-30 05:42:50 +0000719 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000721}
722
723//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000724// BranchInst Implementation
725//===----------------------------------------------------------------------===//
726
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727void BranchInst::AssertOK() {
728 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000729 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000731}
732
Chris Lattner2195fc42007-02-24 00:55:48 +0000733BranchInst::BranchInst(BasicBlock *IfTrue, 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) - 1,
736 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000737 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000738 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000739}
740BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
741 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000742 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000743 OperandTraits<BranchInst>::op_end(this) - 3,
744 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000745 Op<-1>() = IfTrue;
746 Op<-2>() = IfFalse;
747 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000748#ifndef NDEBUG
749 AssertOK();
750#endif
751}
752
753BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000754 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000755 OperandTraits<BranchInst>::op_end(this) - 1,
756 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000757 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000758 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000759}
760
761BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
762 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000763 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000764 OperandTraits<BranchInst>::op_end(this) - 3,
765 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000766 Op<-1>() = IfTrue;
767 Op<-2>() = IfFalse;
768 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000769#ifndef NDEBUG
770 AssertOK();
771#endif
772}
773
774
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000775BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000776 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000777 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
778 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000779 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000780 if (BI.getNumOperands() != 1) {
781 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000782 Op<-3>() = BI.Op<-3>();
783 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000784 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000785 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000786}
787
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000788BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
789 return getSuccessor(idx);
790}
791unsigned BranchInst::getNumSuccessorsV() const {
792 return getNumSuccessors();
793}
794void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
795 setSuccessor(idx, B);
796}
797
798
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000799//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000800// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000801//===----------------------------------------------------------------------===//
802
Owen Andersonb6b25302009-07-14 23:09:55 +0000803static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000804 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000805 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000806 else {
807 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000808 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000809 assert(Amt->getType()->isIntegerTy() &&
810 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000811 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000812 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000813}
814
Chris Lattner229907c2011-07-18 04:54:35 +0000815AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000816 const Twine &Name, Instruction *InsertBefore)
817 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
818 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
819 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000820 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000821 setName(Name);
822}
823
Chris Lattner229907c2011-07-18 04:54:35 +0000824AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000825 const Twine &Name, BasicBlock *InsertAtEnd)
826 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
827 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
828 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000829 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000830 setName(Name);
831}
832
Chris Lattner229907c2011-07-18 04:54:35 +0000833AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000834 Instruction *InsertBefore)
835 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
836 getAISize(Ty->getContext(), 0), InsertBefore) {
837 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000838 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000839 setName(Name);
840}
841
Chris Lattner229907c2011-07-18 04:54:35 +0000842AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000843 BasicBlock *InsertAtEnd)
844 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
845 getAISize(Ty->getContext(), 0), InsertAtEnd) {
846 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000847 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000848 setName(Name);
849}
850
Chris Lattner229907c2011-07-18 04:54:35 +0000851AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000852 const Twine &Name, Instruction *InsertBefore)
853 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000854 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000855 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000856 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000857 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000858}
859
Chris Lattner229907c2011-07-18 04:54:35 +0000860AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000861 const Twine &Name, BasicBlock *InsertAtEnd)
862 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000863 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000864 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000865 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000866 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000867}
868
Gordon Henriksen14a55692007-12-10 02:14:30 +0000869// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000870AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000871}
872
Victor Hernandez8acf2952009-10-23 21:09:37 +0000873void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000874 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000875 assert(Align <= MaximumAlignment &&
876 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000877 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000878 assert(getAlignment() == Align && "Alignment representation error!");
879}
880
Victor Hernandez8acf2952009-10-23 21:09:37 +0000881bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000882 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000883 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000884 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000885}
886
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000887Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000888 return getType()->getElementType();
889}
890
Chris Lattner8b291e62008-11-26 02:54:17 +0000891/// isStaticAlloca - Return true if this alloca is in the entry block of the
892/// function and is a constant size. If so, the code generator will fold it
893/// into the prolog/epilog code, so it is basically free.
894bool AllocaInst::isStaticAlloca() const {
895 // Must be constant size.
896 if (!isa<ConstantInt>(getArraySize())) return false;
897
898 // Must be in the entry block.
899 const BasicBlock *Parent = getParent();
900 return Parent == &Parent->getParent()->front();
901}
902
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000903//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904// LoadInst Implementation
905//===----------------------------------------------------------------------===//
906
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000907void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000908 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000909 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000910 assert(!(isAtomic() && getAlignment() == 0) &&
911 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000912}
913
Daniel Dunbar4975db62009-07-25 04:41:11 +0000914LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000916 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000917 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000918 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000919 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000920 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000921 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922}
923
Daniel Dunbar4975db62009-07-25 04:41:11 +0000924LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000925 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000926 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000927 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000928 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000929 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000930 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000931 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000932}
933
Daniel Dunbar4975db62009-07-25 04:41:11 +0000934LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000935 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000937 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000938 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000939 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000940 setAtomic(NotAtomic);
941 AssertOK();
942 setName(Name);
943}
944
945LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
946 BasicBlock *InsertAE)
947 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
948 Load, Ptr, InsertAE) {
949 setVolatile(isVolatile);
950 setAlignment(0);
951 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +0000952 AssertOK();
953 setName(Name);
954}
955
Daniel Dunbar4975db62009-07-25 04:41:11 +0000956LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000957 unsigned Align, Instruction *InsertBef)
958 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
959 Load, Ptr, InsertBef) {
960 setVolatile(isVolatile);
961 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000962 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000963 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000964 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000965}
966
Daniel Dunbar4975db62009-07-25 04:41:11 +0000967LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000968 unsigned Align, BasicBlock *InsertAE)
969 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
970 Load, Ptr, InsertAE) {
971 setVolatile(isVolatile);
972 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000973 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +0000974 AssertOK();
975 setName(Name);
976}
977
Eli Friedman59b66882011-08-09 23:02:53 +0000978LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
979 unsigned Align, AtomicOrdering Order,
980 SynchronizationScope SynchScope,
981 Instruction *InsertBef)
982 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
983 Load, Ptr, InsertBef) {
984 setVolatile(isVolatile);
985 setAlignment(Align);
986 setAtomic(Order, SynchScope);
987 AssertOK();
988 setName(Name);
989}
990
991LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
992 unsigned Align, AtomicOrdering Order,
993 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000994 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000995 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000996 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000997 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +0000998 setAlignment(Align);
999 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001000 AssertOK();
1001 setName(Name);
1002}
1003
Daniel Dunbar27096822009-08-11 18:11:15 +00001004LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1005 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1006 Load, Ptr, InsertBef) {
1007 setVolatile(false);
1008 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001009 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001010 AssertOK();
1011 if (Name && Name[0]) setName(Name);
1012}
1013
1014LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1015 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1016 Load, Ptr, InsertAE) {
1017 setVolatile(false);
1018 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001019 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001020 AssertOK();
1021 if (Name && Name[0]) setName(Name);
1022}
1023
1024LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1025 Instruction *InsertBef)
1026: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1027 Load, Ptr, InsertBef) {
1028 setVolatile(isVolatile);
1029 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001030 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001031 AssertOK();
1032 if (Name && Name[0]) setName(Name);
1033}
1034
1035LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1036 BasicBlock *InsertAE)
1037 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1038 Load, Ptr, InsertAE) {
1039 setVolatile(isVolatile);
1040 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001041 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001042 AssertOK();
1043 if (Name && Name[0]) setName(Name);
1044}
1045
Christopher Lamb84485702007-04-22 19:24:39 +00001046void LoadInst::setAlignment(unsigned Align) {
1047 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001048 assert(Align <= MaximumAlignment &&
1049 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001050 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001051 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001052 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001053}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001054
1055//===----------------------------------------------------------------------===//
1056// StoreInst Implementation
1057//===----------------------------------------------------------------------===//
1058
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001059void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001060 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001061 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001062 "Ptr must have pointer type!");
1063 assert(getOperand(0)->getType() ==
1064 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001065 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001066 assert(!(isAtomic() && getAlignment() == 0) &&
1067 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001068}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001069
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001070
1071StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001072 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001073 OperandTraits<StoreInst>::op_begin(this),
1074 OperandTraits<StoreInst>::operands(this),
1075 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001076 Op<0>() = val;
1077 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001078 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001079 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001080 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001081 AssertOK();
1082}
1083
1084StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
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 InsertAtEnd) {
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
Misha Brukmanb1c93172005-04-21 23:48:37 +00001097StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001098 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001099 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001100 OperandTraits<StoreInst>::op_begin(this),
1101 OperandTraits<StoreInst>::operands(this),
1102 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001103 Op<0>() = val;
1104 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001105 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001106 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001107 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001108 AssertOK();
1109}
1110
1111StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1112 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001113 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001114 OperandTraits<StoreInst>::op_begin(this),
1115 OperandTraits<StoreInst>::operands(this),
1116 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001117 Op<0>() = val;
1118 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001119 setVolatile(isVolatile);
1120 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001121 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001122 AssertOK();
1123}
1124
Misha Brukmanb1c93172005-04-21 23:48:37 +00001125StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001126 unsigned Align, AtomicOrdering Order,
1127 SynchronizationScope SynchScope,
1128 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001129 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001130 OperandTraits<StoreInst>::op_begin(this),
1131 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001132 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001133 Op<0>() = val;
1134 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001135 setVolatile(isVolatile);
1136 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001137 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001138 AssertOK();
1139}
1140
1141StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001142 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001143 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001144 OperandTraits<StoreInst>::op_begin(this),
1145 OperandTraits<StoreInst>::operands(this),
1146 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001147 Op<0>() = val;
1148 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001149 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001150 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001151 setAtomic(NotAtomic);
1152 AssertOK();
1153}
1154
1155StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1156 unsigned Align, BasicBlock *InsertAtEnd)
1157 : Instruction(Type::getVoidTy(val->getContext()), Store,
1158 OperandTraits<StoreInst>::op_begin(this),
1159 OperandTraits<StoreInst>::operands(this),
1160 InsertAtEnd) {
1161 Op<0>() = val;
1162 Op<1>() = addr;
1163 setVolatile(isVolatile);
1164 setAlignment(Align);
1165 setAtomic(NotAtomic);
1166 AssertOK();
1167}
1168
1169StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1170 unsigned Align, AtomicOrdering Order,
1171 SynchronizationScope SynchScope,
1172 BasicBlock *InsertAtEnd)
1173 : Instruction(Type::getVoidTy(val->getContext()), Store,
1174 OperandTraits<StoreInst>::op_begin(this),
1175 OperandTraits<StoreInst>::operands(this),
1176 InsertAtEnd) {
1177 Op<0>() = val;
1178 Op<1>() = addr;
1179 setVolatile(isVolatile);
1180 setAlignment(Align);
1181 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001182 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001183}
1184
Christopher Lamb84485702007-04-22 19:24:39 +00001185void StoreInst::setAlignment(unsigned Align) {
1186 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001187 assert(Align <= MaximumAlignment &&
1188 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001189 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001190 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001191 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001192}
1193
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001194//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001195// AtomicCmpXchgInst Implementation
1196//===----------------------------------------------------------------------===//
1197
1198void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1199 AtomicOrdering Ordering,
1200 SynchronizationScope SynchScope) {
1201 Op<0>() = Ptr;
1202 Op<1>() = Cmp;
1203 Op<2>() = NewVal;
1204 setOrdering(Ordering);
1205 setSynchScope(SynchScope);
1206
1207 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1208 "All operands must be non-null!");
1209 assert(getOperand(0)->getType()->isPointerTy() &&
1210 "Ptr must have pointer type!");
1211 assert(getOperand(1)->getType() ==
1212 cast<PointerType>(getOperand(0)->getType())->getElementType()
1213 && "Ptr must be a pointer to Cmp type!");
1214 assert(getOperand(2)->getType() ==
1215 cast<PointerType>(getOperand(0)->getType())->getElementType()
1216 && "Ptr must be a pointer to NewVal type!");
1217 assert(Ordering != NotAtomic &&
1218 "AtomicCmpXchg instructions must be atomic!");
1219}
1220
1221AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1222 AtomicOrdering Ordering,
1223 SynchronizationScope SynchScope,
1224 Instruction *InsertBefore)
1225 : Instruction(Cmp->getType(), AtomicCmpXchg,
1226 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1227 OperandTraits<AtomicCmpXchgInst>::operands(this),
1228 InsertBefore) {
1229 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1230}
1231
1232AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1233 AtomicOrdering Ordering,
1234 SynchronizationScope SynchScope,
1235 BasicBlock *InsertAtEnd)
1236 : Instruction(Cmp->getType(), AtomicCmpXchg,
1237 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1238 OperandTraits<AtomicCmpXchgInst>::operands(this),
1239 InsertAtEnd) {
1240 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1241}
1242
1243//===----------------------------------------------------------------------===//
1244// AtomicRMWInst Implementation
1245//===----------------------------------------------------------------------===//
1246
1247void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1248 AtomicOrdering Ordering,
1249 SynchronizationScope SynchScope) {
1250 Op<0>() = Ptr;
1251 Op<1>() = Val;
1252 setOperation(Operation);
1253 setOrdering(Ordering);
1254 setSynchScope(SynchScope);
1255
1256 assert(getOperand(0) && getOperand(1) &&
1257 "All operands must be non-null!");
1258 assert(getOperand(0)->getType()->isPointerTy() &&
1259 "Ptr must have pointer type!");
1260 assert(getOperand(1)->getType() ==
1261 cast<PointerType>(getOperand(0)->getType())->getElementType()
1262 && "Ptr must be a pointer to Val type!");
1263 assert(Ordering != NotAtomic &&
1264 "AtomicRMW instructions must be atomic!");
1265}
1266
1267AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1268 AtomicOrdering Ordering,
1269 SynchronizationScope SynchScope,
1270 Instruction *InsertBefore)
1271 : Instruction(Val->getType(), AtomicRMW,
1272 OperandTraits<AtomicRMWInst>::op_begin(this),
1273 OperandTraits<AtomicRMWInst>::operands(this),
1274 InsertBefore) {
1275 Init(Operation, Ptr, Val, Ordering, SynchScope);
1276}
1277
1278AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1279 AtomicOrdering Ordering,
1280 SynchronizationScope SynchScope,
1281 BasicBlock *InsertAtEnd)
1282 : Instruction(Val->getType(), AtomicRMW,
1283 OperandTraits<AtomicRMWInst>::op_begin(this),
1284 OperandTraits<AtomicRMWInst>::operands(this),
1285 InsertAtEnd) {
1286 Init(Operation, Ptr, Val, Ordering, SynchScope);
1287}
1288
1289//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001290// FenceInst Implementation
1291//===----------------------------------------------------------------------===//
1292
1293FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1294 SynchronizationScope SynchScope,
1295 Instruction *InsertBefore)
1296 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1297 setOrdering(Ordering);
1298 setSynchScope(SynchScope);
1299}
1300
1301FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1302 SynchronizationScope SynchScope,
1303 BasicBlock *InsertAtEnd)
1304 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1305 setOrdering(Ordering);
1306 setSynchScope(SynchScope);
1307}
1308
1309//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001310// GetElementPtrInst Implementation
1311//===----------------------------------------------------------------------===//
1312
Jay Foadd1b78492011-07-25 09:48:08 +00001313void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001314 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001315 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1316 OperandList[0] = Ptr;
1317 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001318 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001319}
1320
Gabor Greiff6caff662008-05-10 08:32:32 +00001321GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001322 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001323 OperandTraits<GetElementPtrInst>::op_end(this)
1324 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001325 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001326 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001327 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001328}
1329
Chris Lattner6090a422009-03-09 04:46:40 +00001330/// getIndexedType - Returns the type of the element that would be accessed with
1331/// a gep instruction with the specified parameters.
1332///
1333/// The Idxs pointer should point to a continuous piece of memory containing the
1334/// indices, either as Value* or uint64_t.
1335///
1336/// A null type is returned if the indices are invalid for the specified
1337/// pointer type.
1338///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001339template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001340static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Chris Lattner229907c2011-07-18 04:54:35 +00001341 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001342 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001343 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001344
Chris Lattner6090a422009-03-09 04:46:40 +00001345 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001346 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001347 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001348
1349 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001350 // it cannot be 'stepped over'.
1351 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001352 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001353
Dan Gohman1ecaf452008-05-31 00:58:22 +00001354 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001355 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001356 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001357 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001358 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001359 if (!CT->indexValid(Index)) return 0;
1360 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001361 }
Jay Foadd1b78492011-07-25 09:48:08 +00001362 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001363}
1364
Jay Foadd1b78492011-07-25 09:48:08 +00001365Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1366 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001367}
1368
Chris Lattner229907c2011-07-18 04:54:35 +00001369Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001370 ArrayRef<Constant *> IdxList) {
1371 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001372}
1373
Jay Foadd1b78492011-07-25 09:48:08 +00001374Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1375 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001376}
1377
Chris Lattner45f15572007-04-14 00:12:57 +00001378/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1379/// zeros. If so, the result pointer and the first operand have the same
1380/// value, just potentially different types.
1381bool GetElementPtrInst::hasAllZeroIndices() const {
1382 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1383 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1384 if (!CI->isZero()) return false;
1385 } else {
1386 return false;
1387 }
1388 }
1389 return true;
1390}
1391
Chris Lattner27058292007-04-27 20:35:56 +00001392/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1393/// constant integers. If so, the result pointer and the first operand have
1394/// a constant offset between them.
1395bool GetElementPtrInst::hasAllConstantIndices() const {
1396 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1397 if (!isa<ConstantInt>(getOperand(i)))
1398 return false;
1399 }
1400 return true;
1401}
1402
Dan Gohman1b849082009-09-07 23:54:19 +00001403void GetElementPtrInst::setIsInBounds(bool B) {
1404 cast<GEPOperator>(this)->setIsInBounds(B);
1405}
Chris Lattner45f15572007-04-14 00:12:57 +00001406
Nick Lewycky28a5f252009-09-27 21:33:04 +00001407bool GetElementPtrInst::isInBounds() const {
1408 return cast<GEPOperator>(this)->isInBounds();
1409}
1410
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001411//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001412// ExtractElementInst Implementation
1413//===----------------------------------------------------------------------===//
1414
1415ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001416 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001417 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001418 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001419 ExtractElement,
1420 OperandTraits<ExtractElementInst>::op_begin(this),
1421 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001422 assert(isValidOperands(Val, Index) &&
1423 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001424 Op<0>() = Val;
1425 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001426 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001427}
1428
1429ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001430 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001431 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001432 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001433 ExtractElement,
1434 OperandTraits<ExtractElementInst>::op_begin(this),
1435 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001436 assert(isValidOperands(Val, Index) &&
1437 "Invalid extractelement instruction operands!");
1438
Gabor Greif2d3024d2008-05-26 21:33:52 +00001439 Op<0>() = Val;
1440 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001441 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001442}
1443
Chris Lattner65511ff2006-10-05 06:24:58 +00001444
Chris Lattner54865b32006-04-08 04:05:48 +00001445bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001446 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001447 return false;
1448 return true;
1449}
1450
1451
Robert Bocchino23004482006-01-10 19:05:34 +00001452//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001453// InsertElementInst Implementation
1454//===----------------------------------------------------------------------===//
1455
Chris Lattner54865b32006-04-08 04:05:48 +00001456InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001457 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001458 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001459 : Instruction(Vec->getType(), InsertElement,
1460 OperandTraits<InsertElementInst>::op_begin(this),
1461 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001462 assert(isValidOperands(Vec, Elt, Index) &&
1463 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001464 Op<0>() = Vec;
1465 Op<1>() = Elt;
1466 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001467 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001468}
1469
Chris Lattner54865b32006-04-08 04:05:48 +00001470InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001471 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001472 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001473 : Instruction(Vec->getType(), InsertElement,
1474 OperandTraits<InsertElementInst>::op_begin(this),
1475 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001476 assert(isValidOperands(Vec, Elt, Index) &&
1477 "Invalid insertelement instruction operands!");
1478
Gabor Greif2d3024d2008-05-26 21:33:52 +00001479 Op<0>() = Vec;
1480 Op<1>() = Elt;
1481 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001482 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001483}
1484
Chris Lattner54865b32006-04-08 04:05:48 +00001485bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1486 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001487 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001488 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001489
Reid Spencerd84d35b2007-02-15 02:26:10 +00001490 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001491 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001492
Duncan Sands9dff9be2010-02-15 16:12:20 +00001493 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001494 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001495 return true;
1496}
1497
1498
Robert Bocchinoca27f032006-01-17 20:07:22 +00001499//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001500// ShuffleVectorInst Implementation
1501//===----------------------------------------------------------------------===//
1502
1503ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001504 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001505 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001506: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001507 cast<VectorType>(Mask->getType())->getNumElements()),
1508 ShuffleVector,
1509 OperandTraits<ShuffleVectorInst>::op_begin(this),
1510 OperandTraits<ShuffleVectorInst>::operands(this),
1511 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001512 assert(isValidOperands(V1, V2, Mask) &&
1513 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001514 Op<0>() = V1;
1515 Op<1>() = V2;
1516 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001517 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001518}
1519
1520ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001521 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001522 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001523: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1524 cast<VectorType>(Mask->getType())->getNumElements()),
1525 ShuffleVector,
1526 OperandTraits<ShuffleVectorInst>::op_begin(this),
1527 OperandTraits<ShuffleVectorInst>::operands(this),
1528 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001529 assert(isValidOperands(V1, V2, Mask) &&
1530 "Invalid shuffle vector instruction operands!");
1531
Gabor Greif2d3024d2008-05-26 21:33:52 +00001532 Op<0>() = V1;
1533 Op<1>() = V2;
1534 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001535 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001536}
1537
Mon P Wang25f01062008-11-10 04:46:22 +00001538bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001539 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001540 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001541 return false;
1542
Chris Lattner229907c2011-07-18 04:54:35 +00001543 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001544 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001545 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001546
1547 // Check to see if Mask is valid.
1548 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001549 VectorType *VTy = cast<VectorType>(V1->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001550 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1551 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1552 if (CI->uge(VTy->getNumElements()*2))
1553 return false;
1554 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1555 return false;
1556 }
1557 }
1558 }
1559 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1560 return false;
1561
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001562 return true;
1563}
1564
Chris Lattnerf724e342008-03-02 05:28:33 +00001565/// getMaskValue - Return the index from the shuffle mask for the specified
1566/// output result. This is either -1 if the element is undef or a number less
1567/// than 2*numelements.
1568int ShuffleVectorInst::getMaskValue(unsigned i) const {
1569 const Constant *Mask = cast<Constant>(getOperand(2));
1570 if (isa<UndefValue>(Mask)) return -1;
1571 if (isa<ConstantAggregateZero>(Mask)) return 0;
1572 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1573 assert(i < MaskCV->getNumOperands() && "Index out of range");
1574
1575 if (isa<UndefValue>(MaskCV->getOperand(i)))
1576 return -1;
1577 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1578}
1579
Dan Gohman12fce772008-05-15 19:50:34 +00001580//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001581// InsertValueInst Class
1582//===----------------------------------------------------------------------===//
1583
Jay Foad57aa6362011-07-13 10:26:04 +00001584void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1585 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001586 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001587
1588 // There's no fundamental reason why we require at least one index
1589 // (other than weirdness with &*IdxBegin being invalid; see
1590 // getelementptr's init routine for example). But there's no
1591 // present need to support it.
1592 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1593
1594 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001595 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001596 Op<0>() = Agg;
1597 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001598
Jay Foad57aa6362011-07-13 10:26:04 +00001599 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001600 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001601}
1602
1603InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001604 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001605 OperandTraits<InsertValueInst>::op_begin(this), 2),
1606 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001607 Op<0>() = IVI.getOperand(0);
1608 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001609 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001610}
1611
1612//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001613// ExtractValueInst Class
1614//===----------------------------------------------------------------------===//
1615
Jay Foad57aa6362011-07-13 10:26:04 +00001616void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001617 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001618
Jay Foad57aa6362011-07-13 10:26:04 +00001619 // There's no fundamental reason why we require at least one index.
1620 // But there's no present need to support it.
1621 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001622
Jay Foad57aa6362011-07-13 10:26:04 +00001623 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001624 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001625}
1626
1627ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001628 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001629 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001630 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001631}
1632
Dan Gohman12fce772008-05-15 19:50:34 +00001633// getIndexedType - Returns the type of the element that would be extracted
1634// with an extractvalue instruction with the specified parameters.
1635//
1636// A null type is returned if the indices are invalid for the specified
1637// pointer type.
1638//
Chris Lattner229907c2011-07-18 04:54:35 +00001639Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001640 ArrayRef<unsigned> Idxs) {
1641 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001642 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001643 // We can't use CompositeType::indexValid(Index) here.
1644 // indexValid() always returns true for arrays because getelementptr allows
1645 // out-of-bounds indices. Since we don't allow those for extractvalue and
1646 // insertvalue we need to check array indexing manually.
1647 // Since the only other types we can index into are struct types it's just
1648 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001649 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001650 if (Index >= AT->getNumElements())
1651 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001652 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001653 if (Index >= ST->getNumElements())
1654 return 0;
1655 } else {
1656 // Not a valid type to index into.
1657 return 0;
1658 }
1659
1660 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001661 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001662 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001663}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001664
1665//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001666// BinaryOperator Class
1667//===----------------------------------------------------------------------===//
1668
Chris Lattner2195fc42007-02-24 00:55:48 +00001669BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001670 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001671 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001672 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001673 OperandTraits<BinaryOperator>::op_begin(this),
1674 OperandTraits<BinaryOperator>::operands(this),
1675 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001676 Op<0>() = S1;
1677 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001678 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001679 setName(Name);
1680}
1681
1682BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001683 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001684 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001685 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001686 OperandTraits<BinaryOperator>::op_begin(this),
1687 OperandTraits<BinaryOperator>::operands(this),
1688 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001689 Op<0>() = S1;
1690 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001691 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001692 setName(Name);
1693}
1694
1695
1696void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001697 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001698 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001699 assert(LHS->getType() == RHS->getType() &&
1700 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001701#ifndef NDEBUG
1702 switch (iType) {
1703 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001704 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001705 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001706 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001707 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001708 "Tried to create an integer operation on a non-integer type!");
1709 break;
1710 case FAdd: case FSub:
1711 case FMul:
1712 assert(getType() == LHS->getType() &&
1713 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001714 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001715 "Tried to create a floating-point operation on a "
1716 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001717 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001718 case UDiv:
1719 case SDiv:
1720 assert(getType() == LHS->getType() &&
1721 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001722 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001723 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001724 "Incorrect operand type (not integer) for S/UDIV");
1725 break;
1726 case FDiv:
1727 assert(getType() == LHS->getType() &&
1728 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001729 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001730 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001731 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001732 case URem:
1733 case SRem:
1734 assert(getType() == LHS->getType() &&
1735 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001736 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001737 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001738 "Incorrect operand type (not integer) for S/UREM");
1739 break;
1740 case FRem:
1741 assert(getType() == LHS->getType() &&
1742 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001743 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001744 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001745 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001746 case Shl:
1747 case LShr:
1748 case AShr:
1749 assert(getType() == LHS->getType() &&
1750 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001751 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001752 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001753 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001754 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001755 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001756 case And: case Or:
1757 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001758 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001759 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001760 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001761 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001762 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001763 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001764 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001765 default:
1766 break;
1767 }
1768#endif
1769}
1770
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001771BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001772 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001773 Instruction *InsertBefore) {
1774 assert(S1->getType() == S2->getType() &&
1775 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001776 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001777}
1778
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001779BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001780 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001781 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001782 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001783 InsertAtEnd->getInstList().push_back(Res);
1784 return Res;
1785}
1786
Dan Gohman5476cfd2009-08-12 16:23:25 +00001787BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001788 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001789 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001790 return new BinaryOperator(Instruction::Sub,
1791 zero, Op,
1792 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001793}
1794
Dan Gohman5476cfd2009-08-12 16:23:25 +00001795BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001796 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001797 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001798 return new BinaryOperator(Instruction::Sub,
1799 zero, Op,
1800 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001801}
1802
Dan Gohman4ab44202009-12-18 02:58:50 +00001803BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1804 Instruction *InsertBefore) {
1805 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1806 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1807}
1808
1809BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1810 BasicBlock *InsertAtEnd) {
1811 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1812 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1813}
1814
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001815BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1816 Instruction *InsertBefore) {
1817 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1818 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1819}
1820
1821BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1822 BasicBlock *InsertAtEnd) {
1823 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1824 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1825}
1826
Dan Gohman5476cfd2009-08-12 16:23:25 +00001827BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001828 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001829 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001830 return new BinaryOperator(Instruction::FSub,
1831 zero, Op,
1832 Op->getType(), Name, InsertBefore);
1833}
1834
Dan Gohman5476cfd2009-08-12 16:23:25 +00001835BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001836 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001837 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001838 return new BinaryOperator(Instruction::FSub,
1839 zero, Op,
1840 Op->getType(), Name, InsertAtEnd);
1841}
1842
Dan Gohman5476cfd2009-08-12 16:23:25 +00001843BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001844 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001845 Constant *C;
Chris Lattner229907c2011-07-18 04:54:35 +00001846 if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001847 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001848 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001849 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001850 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001851 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001852 }
1853
1854 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001855 Op->getType(), Name, InsertBefore);
1856}
1857
Dan Gohman5476cfd2009-08-12 16:23:25 +00001858BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001859 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001860 Constant *AllOnes;
Chris Lattner229907c2011-07-18 04:54:35 +00001861 if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001862 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001863 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001864 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001865 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001866 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001867 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001868 }
1869
1870 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001871 Op->getType(), Name, InsertAtEnd);
1872}
1873
1874
1875// isConstantAllOnes - Helper function for several functions below
1876static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001877 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1878 return CI->isAllOnesValue();
1879 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1880 return CV->isAllOnesValue();
1881 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001882}
1883
Owen Andersonbb2501b2009-07-13 22:18:28 +00001884bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001885 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1886 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001887 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1888 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001889 return false;
1890}
1891
Owen Andersonbb2501b2009-07-13 22:18:28 +00001892bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001893 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1894 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001895 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001896 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001897 return false;
1898}
1899
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001900bool BinaryOperator::isNot(const Value *V) {
1901 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1902 return (Bop->getOpcode() == Instruction::Xor &&
1903 (isConstantAllOnes(Bop->getOperand(1)) ||
1904 isConstantAllOnes(Bop->getOperand(0))));
1905 return false;
1906}
1907
Chris Lattner2c7d1772005-04-24 07:28:37 +00001908Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001909 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001910}
1911
Chris Lattner2c7d1772005-04-24 07:28:37 +00001912const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1913 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001914}
1915
Dan Gohmana5b96452009-06-04 22:49:04 +00001916Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001917 return cast<BinaryOperator>(BinOp)->getOperand(1);
1918}
1919
1920const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1921 return getFNegArgument(const_cast<Value*>(BinOp));
1922}
1923
Chris Lattner2c7d1772005-04-24 07:28:37 +00001924Value *BinaryOperator::getNotArgument(Value *BinOp) {
1925 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1926 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1927 Value *Op0 = BO->getOperand(0);
1928 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001929 if (isConstantAllOnes(Op0)) return Op1;
1930
1931 assert(isConstantAllOnes(Op1));
1932 return Op0;
1933}
1934
Chris Lattner2c7d1772005-04-24 07:28:37 +00001935const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1936 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001937}
1938
1939
1940// swapOperands - Exchange the two operands to this instruction. This
1941// instruction is safe to use on any binary instruction and does not
1942// modify the semantics of the instruction. If the instruction is
1943// order dependent (SetLT f.e.) the opcode is changed.
1944//
1945bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001946 if (!isCommutative())
1947 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001948 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001949 return false;
1950}
1951
Dan Gohman1b849082009-09-07 23:54:19 +00001952void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1953 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1954}
1955
1956void BinaryOperator::setHasNoSignedWrap(bool b) {
1957 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1958}
1959
1960void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001961 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001962}
1963
Nick Lewycky28a5f252009-09-27 21:33:04 +00001964bool BinaryOperator::hasNoUnsignedWrap() const {
1965 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1966}
1967
1968bool BinaryOperator::hasNoSignedWrap() const {
1969 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1970}
1971
1972bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001973 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001974}
1975
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001976//===----------------------------------------------------------------------===//
1977// CastInst Class
1978//===----------------------------------------------------------------------===//
1979
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001980// Just determine if this cast only deals with integral->integral conversion.
1981bool CastInst::isIntegerCast() const {
1982 switch (getOpcode()) {
1983 default: return false;
1984 case Instruction::ZExt:
1985 case Instruction::SExt:
1986 case Instruction::Trunc:
1987 return true;
1988 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001989 return getOperand(0)->getType()->isIntegerTy() &&
1990 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001991 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001992}
1993
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001994bool CastInst::isLosslessCast() const {
1995 // Only BitCast can be lossless, exit fast if we're not BitCast
1996 if (getOpcode() != Instruction::BitCast)
1997 return false;
1998
1999 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002000 Type* SrcTy = getOperand(0)->getType();
2001 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002002 if (SrcTy == DstTy)
2003 return true;
2004
Reid Spencer8d9336d2006-12-31 05:26:44 +00002005 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002006 if (SrcTy->isPointerTy())
2007 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002008 return false; // Other types have no identity values
2009}
2010
2011/// This function determines if the CastInst does not require any bits to be
2012/// changed in order to effect the cast. Essentially, it identifies cases where
2013/// no code gen is necessary for the cast, hence the name no-op cast. For
2014/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002015/// # bitcast i32* %x to i8*
2016/// # bitcast <2 x i32> %x to <4 x i16>
2017/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002018/// @brief Determine if the described cast is a no-op.
2019bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002020 Type *SrcTy,
2021 Type *DestTy,
2022 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002023 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002024 default:
2025 assert(!"Invalid CastOp");
2026 case Instruction::Trunc:
2027 case Instruction::ZExt:
2028 case Instruction::SExt:
2029 case Instruction::FPTrunc:
2030 case Instruction::FPExt:
2031 case Instruction::UIToFP:
2032 case Instruction::SIToFP:
2033 case Instruction::FPToUI:
2034 case Instruction::FPToSI:
2035 return false; // These always modify bits
2036 case Instruction::BitCast:
2037 return true; // BitCast never modifies bits.
2038 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002039 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002040 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002041 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002042 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002043 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044 }
2045}
2046
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002047/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002048bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002049 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2050}
2051
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002052/// This function determines if a pair of casts can be eliminated and what
2053/// opcode should be used in the elimination. This assumes that there are two
2054/// instructions like this:
2055/// * %F = firstOpcode SrcTy %x to MidTy
2056/// * %S = secondOpcode MidTy %F to DstTy
2057/// The function returns a resultOpcode so these two casts can be replaced with:
2058/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2059/// If no such cast is permited, the function returns 0.
2060unsigned CastInst::isEliminableCastPair(
2061 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Chris Lattner229907c2011-07-18 04:54:35 +00002062 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063{
2064 // Define the 144 possibilities for these two cast instructions. The values
2065 // in this matrix determine what to do in a given situation and select the
2066 // case in the switch below. The rows correspond to firstOp, the columns
2067 // correspond to secondOp. In looking at the table below, keep in mind
2068 // the following cast properties:
2069 //
2070 // Size Compare Source Destination
2071 // Operator Src ? Size Type Sign Type Sign
2072 // -------- ------------ ------------------- ---------------------
2073 // TRUNC > Integer Any Integral Any
2074 // ZEXT < Integral Unsigned Integer Any
2075 // SEXT < Integral Signed Integer Any
2076 // FPTOUI n/a FloatPt n/a Integral Unsigned
2077 // FPTOSI n/a FloatPt n/a Integral Signed
2078 // UITOFP n/a Integral Unsigned FloatPt n/a
2079 // SITOFP n/a Integral Signed FloatPt n/a
2080 // FPTRUNC > FloatPt n/a FloatPt n/a
2081 // FPEXT < FloatPt n/a FloatPt n/a
2082 // PTRTOINT n/a Pointer n/a Integral Unsigned
2083 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002084 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002085 //
2086 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002087 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2088 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002089 // of the produced value (we no longer know the top-part is all zeros).
2090 // Further this conversion is often much more expensive for typical hardware,
2091 // and causes issues when building libgcc. We disallow fptosi+sext for the
2092 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002093 const unsigned numCastOps =
2094 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2095 static const uint8_t CastResults[numCastOps][numCastOps] = {
2096 // T F F U S F F P I B -+
2097 // R Z S P P I I T P 2 N T |
2098 // U E E 2 2 2 2 R E I T C +- secondOp
2099 // N X X U S F F N X N 2 V |
2100 // C T T I I P P C T T P T -+
2101 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2102 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2103 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002104 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2105 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002106 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2107 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2108 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2109 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2110 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2111 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2112 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2113 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002114
2115 // If either of the casts are a bitcast from scalar to vector, disallow the
2116 // merging.
2117 if ((firstOp == Instruction::BitCast &&
2118 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2119 (secondOp == Instruction::BitCast &&
2120 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2121 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002122
2123 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2124 [secondOp-Instruction::CastOpsBegin];
2125 switch (ElimCase) {
2126 case 0:
2127 // categorically disallowed
2128 return 0;
2129 case 1:
2130 // allowed, use first cast's opcode
2131 return firstOp;
2132 case 2:
2133 // allowed, use second cast's opcode
2134 return secondOp;
2135 case 3:
2136 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002137 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002138 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002139 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002140 return firstOp;
2141 return 0;
2142 case 4:
2143 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002144 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002145 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002146 return firstOp;
2147 return 0;
2148 case 5:
2149 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002150 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002151 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002152 return secondOp;
2153 return 0;
2154 case 6:
2155 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002156 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002157 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002158 return secondOp;
2159 return 0;
2160 case 7: {
2161 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002162 if (!IntPtrTy)
2163 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002164 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2165 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166 if (MidSize >= PtrSize)
2167 return Instruction::BitCast;
2168 return 0;
2169 }
2170 case 8: {
2171 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2172 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2173 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002174 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2175 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176 if (SrcSize == DstSize)
2177 return Instruction::BitCast;
2178 else if (SrcSize < DstSize)
2179 return firstOp;
2180 return secondOp;
2181 }
2182 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2183 return Instruction::ZExt;
2184 case 10:
2185 // fpext followed by ftrunc is allowed if the bit size returned to is
2186 // the same as the original, in which case its just a bitcast
2187 if (SrcTy == DstTy)
2188 return Instruction::BitCast;
2189 return 0; // If the types are not the same we can't eliminate it.
2190 case 11:
2191 // bitcast followed by ptrtoint is allowed as long as the bitcast
2192 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002193 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002194 return secondOp;
2195 return 0;
2196 case 12:
2197 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002198 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002199 return firstOp;
2200 return 0;
2201 case 13: {
2202 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002203 if (!IntPtrTy)
2204 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002205 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2206 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2207 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002208 if (SrcSize <= PtrSize && SrcSize == DstSize)
2209 return Instruction::BitCast;
2210 return 0;
2211 }
2212 case 99:
2213 // cast combination can't happen (error in input). This is for all cases
2214 // where the MidTy is not the same for the two cast instructions.
2215 assert(!"Invalid Cast Combination");
2216 return 0;
2217 default:
2218 assert(!"Error in CastResults table!!!");
2219 return 0;
2220 }
2221 return 0;
2222}
2223
Chris Lattner229907c2011-07-18 04:54:35 +00002224CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002225 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002226 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002227 // Construct and return the appropriate CastInst subclass
2228 switch (op) {
2229 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2230 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2231 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2232 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2233 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2234 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2235 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2236 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2237 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2238 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2239 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2240 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2241 default:
2242 assert(!"Invalid opcode provided");
2243 }
2244 return 0;
2245}
2246
Chris Lattner229907c2011-07-18 04:54:35 +00002247CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002248 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002249 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250 // Construct and return the appropriate CastInst subclass
2251 switch (op) {
2252 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2253 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2254 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2255 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2256 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2257 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2258 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2259 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2260 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2261 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2262 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2263 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2264 default:
2265 assert(!"Invalid opcode provided");
2266 }
2267 return 0;
2268}
2269
Chris Lattner229907c2011-07-18 04:54:35 +00002270CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002271 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002272 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002273 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002274 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2275 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002276}
2277
Chris Lattner229907c2011-07-18 04:54:35 +00002278CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002279 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002280 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002281 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002282 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2283 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002284}
2285
Chris Lattner229907c2011-07-18 04:54:35 +00002286CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002287 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002288 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002289 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002290 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2291 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002292}
2293
Chris Lattner229907c2011-07-18 04:54:35 +00002294CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002295 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002296 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002297 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002298 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2299 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002300}
2301
Chris Lattner229907c2011-07-18 04:54:35 +00002302CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002303 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002304 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002305 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002306 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2307 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002308}
2309
Chris Lattner229907c2011-07-18 04:54:35 +00002310CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002311 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002312 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002313 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002314 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2315 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002316}
2317
Chris Lattner229907c2011-07-18 04:54:35 +00002318CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002319 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002320 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002321 assert(S->getType()->isPointerTy() && "Invalid cast");
2322 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002323 "Invalid cast");
2324
Duncan Sands9dff9be2010-02-15 16:12:20 +00002325 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002326 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2327 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002328}
2329
2330/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002331CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002332 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002333 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002334 assert(S->getType()->isPointerTy() && "Invalid cast");
2335 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002336 "Invalid cast");
2337
Duncan Sands9dff9be2010-02-15 16:12:20 +00002338 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002339 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2340 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002341}
2342
Chris Lattner229907c2011-07-18 04:54:35 +00002343CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002344 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002345 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002346 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002347 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002348 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2349 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002350 Instruction::CastOps opcode =
2351 (SrcBits == DstBits ? Instruction::BitCast :
2352 (SrcBits > DstBits ? Instruction::Trunc :
2353 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002354 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002355}
2356
Chris Lattner229907c2011-07-18 04:54:35 +00002357CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002358 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002359 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002360 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002361 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002362 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2363 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002364 Instruction::CastOps opcode =
2365 (SrcBits == DstBits ? Instruction::BitCast :
2366 (SrcBits > DstBits ? Instruction::Trunc :
2367 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002368 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002369}
2370
Chris Lattner229907c2011-07-18 04:54:35 +00002371CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002372 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002373 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002374 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002375 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002376 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2377 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002378 Instruction::CastOps opcode =
2379 (SrcBits == DstBits ? Instruction::BitCast :
2380 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002381 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002382}
2383
Chris Lattner229907c2011-07-18 04:54:35 +00002384CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002385 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002386 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002387 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002388 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002389 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2390 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002391 Instruction::CastOps opcode =
2392 (SrcBits == DstBits ? Instruction::BitCast :
2393 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002394 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002395}
2396
Duncan Sands55e50902008-01-06 10:12:28 +00002397// Check whether it is valid to call getCastOpcode for these types.
2398// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002399bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002400 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2401 return false;
2402
2403 if (SrcTy == DestTy)
2404 return true;
2405
Chris Lattner229907c2011-07-18 04:54:35 +00002406 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2407 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002408 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2409 // An element by element cast. Valid if casting the elements is valid.
2410 SrcTy = SrcVecTy->getElementType();
2411 DestTy = DestVecTy->getElementType();
2412 }
2413
Duncan Sands55e50902008-01-06 10:12:28 +00002414 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002415 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2416 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002417
2418 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002419 if (DestTy->isIntegerTy()) { // Casting to integral
2420 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002421 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002422 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002423 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002424 } else if (SrcTy->isVectorTy()) { // Casting from vector
2425 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002426 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002427 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002428 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002429 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2430 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002431 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002432 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002433 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002434 } else if (SrcTy->isVectorTy()) { // Casting from vector
2435 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002436 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002437 return false;
2438 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002439 } else if (DestTy->isVectorTy()) { // Casting to vector
2440 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002441 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002442 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002443 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002444 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002445 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002446 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002447 return false;
2448 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002449 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002450 if (SrcTy->isVectorTy()) {
2451 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002452 } else {
2453 return false;
2454 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002455 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002456 return false;
2457 }
2458}
2459
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460// Provide a way to get a "cast" where the cast opcode is inferred from the
2461// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002462// logic in the castIsValid function below. This axiom should hold:
2463// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2464// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002466// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002468CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002469 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2470 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471
Duncan Sands55e50902008-01-06 10:12:28 +00002472 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2473 "Only first class types are castable!");
2474
Duncan Sandsa8514532011-05-18 07:13:41 +00002475 if (SrcTy == DestTy)
2476 return BitCast;
2477
Chris Lattner229907c2011-07-18 04:54:35 +00002478 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2479 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002480 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2481 // An element by element cast. Find the appropriate opcode based on the
2482 // element types.
2483 SrcTy = SrcVecTy->getElementType();
2484 DestTy = DestVecTy->getElementType();
2485 }
2486
2487 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002488 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2489 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002490
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002492 if (DestTy->isIntegerTy()) { // Casting to integral
2493 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494 if (DestBits < SrcBits)
2495 return Trunc; // int -> smaller int
2496 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002497 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498 return SExt; // signed -> SEXT
2499 else
2500 return ZExt; // unsigned -> ZEXT
2501 } else {
2502 return BitCast; // Same size, No-op cast
2503 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002504 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002505 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002506 return FPToSI; // FP -> sint
2507 else
2508 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002509 } else if (SrcTy->isVectorTy()) {
2510 assert(DestBits == SrcBits &&
2511 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512 return BitCast; // Same size, no-op cast
2513 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002514 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515 "Casting from a value that is not first-class type");
2516 return PtrToInt; // ptr -> int
2517 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002518 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2519 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002520 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521 return SIToFP; // sint -> FP
2522 else
2523 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002524 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002525 if (DestBits < SrcBits) {
2526 return FPTrunc; // FP -> smaller FP
2527 } else if (DestBits > SrcBits) {
2528 return FPExt; // FP -> larger FP
2529 } else {
2530 return BitCast; // same size, no-op cast
2531 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002532 } else if (SrcTy->isVectorTy()) {
2533 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002534 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002535 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002537 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002539 } else if (DestTy->isVectorTy()) {
2540 assert(DestBits == SrcBits &&
2541 "Illegal cast to vector (wrong type or size)");
2542 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002543 } else if (DestTy->isPointerTy()) {
2544 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002546 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547 return IntToPtr; // int -> ptr
2548 } else {
2549 assert(!"Casting pointer to other than pointer or int");
2550 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002551 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002552 if (SrcTy->isVectorTy()) {
2553 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002554 return BitCast; // 64-bit vector to MMX
2555 } else {
2556 assert(!"Illegal cast to X86_MMX");
2557 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558 } else {
2559 assert(!"Casting to type that is not first-class");
2560 }
2561
2562 // If we fall through to here we probably hit an assertion cast above
2563 // and assertions are not turned on. Anything we return is an error, so
2564 // BitCast is as good a choice as any.
2565 return BitCast;
2566}
2567
2568//===----------------------------------------------------------------------===//
2569// CastInst SubClass Constructors
2570//===----------------------------------------------------------------------===//
2571
2572/// Check that the construction parameters for a CastInst are correct. This
2573/// could be broken out into the separate constructors but it is useful to have
2574/// it in one place and to eliminate the redundant code for getting the sizes
2575/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002576bool
Chris Lattner229907c2011-07-18 04:54:35 +00002577CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002578
2579 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002580 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002581 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2582 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583 return false;
2584
2585 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002586 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2587 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588
Duncan Sands7f646562011-05-18 09:21:57 +00002589 // If these are vector types, get the lengths of the vectors (using zero for
2590 // scalar types means that checking that vector lengths match also checks that
2591 // scalars are not being converted to vectors or vectors to scalars).
2592 unsigned SrcLength = SrcTy->isVectorTy() ?
2593 cast<VectorType>(SrcTy)->getNumElements() : 0;
2594 unsigned DstLength = DstTy->isVectorTy() ?
2595 cast<VectorType>(DstTy)->getNumElements() : 0;
2596
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597 // Switch on the opcode provided
2598 switch (op) {
2599 default: return false; // This is an input error
2600 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002601 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2602 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002604 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2605 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002607 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2608 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002610 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2611 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002613 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2614 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002617 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2618 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002621 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2622 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002624 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002625 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002626 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002627 case Instruction::BitCast:
2628 // BitCast implies a no-op cast of type only. No bits change.
2629 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002630 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631 return false;
2632
Duncan Sands55e50902008-01-06 10:12:28 +00002633 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634 // these cases, the cast is okay if the source and destination bit widths
2635 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002636 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637 }
2638}
2639
2640TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002641 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002643 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002644}
2645
2646TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002647 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002649 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002650}
2651
2652ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002653 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002655 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002656}
2657
2658ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002659 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002661 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002662}
2663SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002664 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002666 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002667}
2668
Jeff Cohencc08c832006-12-02 02:22:01 +00002669SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002670 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002672 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002673}
2674
2675FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002676 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002678 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002679}
2680
2681FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002682 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002683) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002684 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002685}
2686
2687FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002688 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002689) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002690 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002691}
2692
2693FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002694 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002695) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002696 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002697}
2698
2699UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002700 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002701) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002702 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002703}
2704
2705UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002706 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002707) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002708 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002709}
2710
2711SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002712 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002713) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002714 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002715}
2716
2717SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002718 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002719) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002720 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002721}
2722
2723FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002724 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002725) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002726 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002727}
2728
2729FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002730 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002731) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002732 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002733}
2734
2735FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002736 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002737) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002738 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739}
2740
2741FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002742 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002743) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002744 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002745}
2746
2747PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002748 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002749) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002750 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002751}
2752
2753PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002754 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002755) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002756 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002757}
2758
2759IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002760 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002761) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002762 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002763}
2764
2765IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002766 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002767) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002768 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002769}
2770
2771BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002772 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002773) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002774 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002775}
2776
2777BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002778 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002779) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002780 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002781}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002782
2783//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002784// CmpInst Classes
2785//===----------------------------------------------------------------------===//
2786
Chris Lattneraec33da2010-01-22 06:25:37 +00002787void CmpInst::Anchor() const {}
2788
Chris Lattner229907c2011-07-18 04:54:35 +00002789CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002790 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002791 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002792 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002793 OperandTraits<CmpInst>::op_begin(this),
2794 OperandTraits<CmpInst>::operands(this),
2795 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002796 Op<0>() = LHS;
2797 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002798 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002799 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002800}
Gabor Greiff6caff662008-05-10 08:32:32 +00002801
Chris Lattner229907c2011-07-18 04:54:35 +00002802CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002803 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002804 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002805 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002806 OperandTraits<CmpInst>::op_begin(this),
2807 OperandTraits<CmpInst>::operands(this),
2808 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002809 Op<0>() = LHS;
2810 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002811 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002812 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002813}
2814
2815CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002816CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002817 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002818 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002819 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002820 if (InsertBefore)
2821 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2822 S1, S2, Name);
2823 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002824 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002825 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002826 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002827
2828 if (InsertBefore)
2829 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2830 S1, S2, Name);
2831 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002832 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002833 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002834}
2835
2836CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002837CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002838 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002839 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002840 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2841 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002842 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002843 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2844 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002845}
2846
2847void CmpInst::swapOperands() {
2848 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2849 IC->swapOperands();
2850 else
2851 cast<FCmpInst>(this)->swapOperands();
2852}
2853
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002854bool CmpInst::isCommutative() const {
2855 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002856 return IC->isCommutative();
2857 return cast<FCmpInst>(this)->isCommutative();
2858}
2859
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002860bool CmpInst::isEquality() const {
2861 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002862 return IC->isEquality();
2863 return cast<FCmpInst>(this)->isEquality();
2864}
2865
2866
Dan Gohman4e724382008-05-31 02:47:54 +00002867CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002868 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002869 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002870 case ICMP_EQ: return ICMP_NE;
2871 case ICMP_NE: return ICMP_EQ;
2872 case ICMP_UGT: return ICMP_ULE;
2873 case ICMP_ULT: return ICMP_UGE;
2874 case ICMP_UGE: return ICMP_ULT;
2875 case ICMP_ULE: return ICMP_UGT;
2876 case ICMP_SGT: return ICMP_SLE;
2877 case ICMP_SLT: return ICMP_SGE;
2878 case ICMP_SGE: return ICMP_SLT;
2879 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002880
Dan Gohman4e724382008-05-31 02:47:54 +00002881 case FCMP_OEQ: return FCMP_UNE;
2882 case FCMP_ONE: return FCMP_UEQ;
2883 case FCMP_OGT: return FCMP_ULE;
2884 case FCMP_OLT: return FCMP_UGE;
2885 case FCMP_OGE: return FCMP_ULT;
2886 case FCMP_OLE: return FCMP_UGT;
2887 case FCMP_UEQ: return FCMP_ONE;
2888 case FCMP_UNE: return FCMP_OEQ;
2889 case FCMP_UGT: return FCMP_OLE;
2890 case FCMP_ULT: return FCMP_OGE;
2891 case FCMP_UGE: return FCMP_OLT;
2892 case FCMP_ULE: return FCMP_OGT;
2893 case FCMP_ORD: return FCMP_UNO;
2894 case FCMP_UNO: return FCMP_ORD;
2895 case FCMP_TRUE: return FCMP_FALSE;
2896 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002897 }
2898}
2899
Reid Spencer266e42b2006-12-23 06:05:41 +00002900ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2901 switch (pred) {
2902 default: assert(! "Unknown icmp predicate!");
2903 case ICMP_EQ: case ICMP_NE:
2904 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2905 return pred;
2906 case ICMP_UGT: return ICMP_SGT;
2907 case ICMP_ULT: return ICMP_SLT;
2908 case ICMP_UGE: return ICMP_SGE;
2909 case ICMP_ULE: return ICMP_SLE;
2910 }
2911}
2912
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002913ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2914 switch (pred) {
2915 default: assert(! "Unknown icmp predicate!");
2916 case ICMP_EQ: case ICMP_NE:
2917 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2918 return pred;
2919 case ICMP_SGT: return ICMP_UGT;
2920 case ICMP_SLT: return ICMP_ULT;
2921 case ICMP_SGE: return ICMP_UGE;
2922 case ICMP_SLE: return ICMP_ULE;
2923 }
2924}
2925
Reid Spencer0286bc12007-02-28 22:00:54 +00002926/// Initialize a set of values that all satisfy the condition with C.
2927///
2928ConstantRange
2929ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2930 APInt Lower(C);
2931 APInt Upper(C);
2932 uint32_t BitWidth = C.getBitWidth();
2933 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002934 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002935 case ICmpInst::ICMP_EQ: Upper++; break;
2936 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002937 case ICmpInst::ICMP_ULT:
2938 Lower = APInt::getMinValue(BitWidth);
2939 // Check for an empty-set condition.
2940 if (Lower == Upper)
2941 return ConstantRange(BitWidth, /*isFullSet=*/false);
2942 break;
2943 case ICmpInst::ICMP_SLT:
2944 Lower = APInt::getSignedMinValue(BitWidth);
2945 // Check for an empty-set condition.
2946 if (Lower == Upper)
2947 return ConstantRange(BitWidth, /*isFullSet=*/false);
2948 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002949 case ICmpInst::ICMP_UGT:
2950 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002951 // Check for an empty-set condition.
2952 if (Lower == Upper)
2953 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002954 break;
2955 case ICmpInst::ICMP_SGT:
2956 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002957 // Check for an empty-set condition.
2958 if (Lower == Upper)
2959 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002960 break;
2961 case ICmpInst::ICMP_ULE:
2962 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002963 // Check for a full-set condition.
2964 if (Lower == Upper)
2965 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002966 break;
2967 case ICmpInst::ICMP_SLE:
2968 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002969 // Check for a full-set condition.
2970 if (Lower == Upper)
2971 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002972 break;
2973 case ICmpInst::ICMP_UGE:
2974 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002975 // Check for a full-set condition.
2976 if (Lower == Upper)
2977 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002978 break;
2979 case ICmpInst::ICMP_SGE:
2980 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002981 // Check for a full-set condition.
2982 if (Lower == Upper)
2983 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002984 break;
2985 }
2986 return ConstantRange(Lower, Upper);
2987}
2988
Dan Gohman4e724382008-05-31 02:47:54 +00002989CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002990 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002991 default: assert(!"Unknown cmp predicate!");
2992 case ICMP_EQ: case ICMP_NE:
2993 return pred;
2994 case ICMP_SGT: return ICMP_SLT;
2995 case ICMP_SLT: return ICMP_SGT;
2996 case ICMP_SGE: return ICMP_SLE;
2997 case ICMP_SLE: return ICMP_SGE;
2998 case ICMP_UGT: return ICMP_ULT;
2999 case ICMP_ULT: return ICMP_UGT;
3000 case ICMP_UGE: return ICMP_ULE;
3001 case ICMP_ULE: return ICMP_UGE;
3002
Reid Spencerd9436b62006-11-20 01:22:35 +00003003 case FCMP_FALSE: case FCMP_TRUE:
3004 case FCMP_OEQ: case FCMP_ONE:
3005 case FCMP_UEQ: case FCMP_UNE:
3006 case FCMP_ORD: case FCMP_UNO:
3007 return pred;
3008 case FCMP_OGT: return FCMP_OLT;
3009 case FCMP_OLT: return FCMP_OGT;
3010 case FCMP_OGE: return FCMP_OLE;
3011 case FCMP_OLE: return FCMP_OGE;
3012 case FCMP_UGT: return FCMP_ULT;
3013 case FCMP_ULT: return FCMP_UGT;
3014 case FCMP_UGE: return FCMP_ULE;
3015 case FCMP_ULE: return FCMP_UGE;
3016 }
3017}
3018
Reid Spencer266e42b2006-12-23 06:05:41 +00003019bool CmpInst::isUnsigned(unsigned short predicate) {
3020 switch (predicate) {
3021 default: return false;
3022 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3023 case ICmpInst::ICMP_UGE: return true;
3024 }
3025}
3026
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003027bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003028 switch (predicate) {
3029 default: return false;
3030 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3031 case ICmpInst::ICMP_SGE: return true;
3032 }
3033}
3034
3035bool CmpInst::isOrdered(unsigned short predicate) {
3036 switch (predicate) {
3037 default: return false;
3038 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3039 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3040 case FCmpInst::FCMP_ORD: return true;
3041 }
3042}
3043
3044bool CmpInst::isUnordered(unsigned short predicate) {
3045 switch (predicate) {
3046 default: return false;
3047 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3048 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3049 case FCmpInst::FCMP_UNO: return true;
3050 }
3051}
3052
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003053bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3054 switch(predicate) {
3055 default: return false;
3056 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3057 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3058 }
3059}
3060
3061bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3062 switch(predicate) {
3063 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3064 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3065 default: return false;
3066 }
3067}
3068
3069
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003070//===----------------------------------------------------------------------===//
3071// SwitchInst Implementation
3072//===----------------------------------------------------------------------===//
3073
Chris Lattnerbaf00152010-11-17 05:41:46 +00003074void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3075 assert(Value && Default && NumReserved);
3076 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003077 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003078 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003079
Gabor Greif2d3024d2008-05-26 21:33:52 +00003080 OperandList[0] = Value;
3081 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003082}
3083
Chris Lattner2195fc42007-02-24 00:55:48 +00003084/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3085/// switch on and a default destination. The number of additional cases can
3086/// be specified here to make memory allocation more efficient. This
3087/// constructor can also autoinsert before another instruction.
3088SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3089 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003090 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3091 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003092 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003093}
3094
3095/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3096/// switch on and a default destination. The number of additional cases can
3097/// be specified here to make memory allocation more efficient. This
3098/// constructor also autoinserts at the end of the specified BasicBlock.
3099SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3100 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003101 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3102 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003103 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003104}
3105
Misha Brukmanb1c93172005-04-21 23:48:37 +00003106SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003107 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3108 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3109 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003110 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003111 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003112 OL[i] = InOL[i];
3113 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003114 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003115 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003116}
3117
Gordon Henriksen14a55692007-12-10 02:14:30 +00003118SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003119 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003120}
3121
3122
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003123/// addCase - Add an entry to the switch instruction...
3124///
Chris Lattner47ac1872005-02-24 05:32:09 +00003125void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003126 unsigned OpNo = NumOperands;
3127 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003128 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003129 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003130 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003131 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003132 OperandList[OpNo] = OnVal;
3133 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003134}
3135
3136/// removeCase - This method removes the specified successor from the switch
3137/// instruction. Note that this cannot be used to remove the default
3138/// destination (successor #0).
3139///
3140void SwitchInst::removeCase(unsigned idx) {
3141 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003142 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3143
3144 unsigned NumOps = getNumOperands();
3145 Use *OL = OperandList;
3146
Jay Foad14277722011-02-01 09:22:34 +00003147 // Overwrite this case with the end of the list.
3148 if ((idx + 1) * 2 != NumOps) {
3149 OL[idx * 2] = OL[NumOps - 2];
3150 OL[idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003151 }
3152
3153 // Nuke the last value.
3154 OL[NumOps-2].set(0);
3155 OL[NumOps-2+1].set(0);
3156 NumOperands = NumOps-2;
3157}
3158
Jay Foade98f29d2011-04-01 08:00:58 +00003159/// growOperands - grow operands - This grows the operand list in response
3160/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003161///
Jay Foade98f29d2011-04-01 08:00:58 +00003162void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003163 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003164 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003165
3166 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003167 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003168 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003169 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003170 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003171 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003172 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003173 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003174}
3175
3176
3177BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3178 return getSuccessor(idx);
3179}
3180unsigned SwitchInst::getNumSuccessorsV() const {
3181 return getNumSuccessors();
3182}
3183void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3184 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003185}
Chris Lattnerf22be932004-10-15 23:52:53 +00003186
Chris Lattner3ed871f2009-10-27 19:13:16 +00003187//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003188// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003189//===----------------------------------------------------------------------===//
3190
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003191void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003192 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003193 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003194 ReservedSpace = 1+NumDests;
3195 NumOperands = 1;
3196 OperandList = allocHungoffUses(ReservedSpace);
3197
3198 OperandList[0] = Address;
3199}
3200
3201
Jay Foade98f29d2011-04-01 08:00:58 +00003202/// growOperands - grow operands - This grows the operand list in response
3203/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003204///
Jay Foade98f29d2011-04-01 08:00:58 +00003205void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003206 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003207 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003208
3209 ReservedSpace = NumOps;
3210 Use *NewOps = allocHungoffUses(NumOps);
3211 Use *OldOps = OperandList;
3212 for (unsigned i = 0; i != e; ++i)
3213 NewOps[i] = OldOps[i];
3214 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003215 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003216}
3217
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003218IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3219 Instruction *InsertBefore)
3220: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003221 0, 0, InsertBefore) {
3222 init(Address, NumCases);
3223}
3224
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003225IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3226 BasicBlock *InsertAtEnd)
3227: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003228 0, 0, InsertAtEnd) {
3229 init(Address, NumCases);
3230}
3231
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003232IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3233 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003234 allocHungoffUses(IBI.getNumOperands()),
3235 IBI.getNumOperands()) {
3236 Use *OL = OperandList, *InOL = IBI.OperandList;
3237 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3238 OL[i] = InOL[i];
3239 SubclassOptionalData = IBI.SubclassOptionalData;
3240}
3241
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003242IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003243 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003244}
3245
3246/// addDestination - Add a destination.
3247///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003248void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003249 unsigned OpNo = NumOperands;
3250 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003251 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003252 // Initialize some new operands.
3253 assert(OpNo < ReservedSpace && "Growing didn't work!");
3254 NumOperands = OpNo+1;
3255 OperandList[OpNo] = DestBB;
3256}
3257
3258/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003259/// indirectbr instruction.
3260void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003261 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3262
3263 unsigned NumOps = getNumOperands();
3264 Use *OL = OperandList;
3265
3266 // Replace this value with the last one.
3267 OL[idx+1] = OL[NumOps-1];
3268
3269 // Nuke the last value.
3270 OL[NumOps-1].set(0);
3271 NumOperands = NumOps-1;
3272}
3273
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003274BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003275 return getSuccessor(idx);
3276}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003277unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003278 return getNumSuccessors();
3279}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003280void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003281 setSuccessor(idx, B);
3282}
3283
3284//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003285// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003286//===----------------------------------------------------------------------===//
3287
Chris Lattnerf22be932004-10-15 23:52:53 +00003288// Define these methods here so vtables don't get emitted into every translation
3289// unit that uses these classes.
3290
Devang Patel11cf3f42009-10-27 22:16:29 +00003291GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3292 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003293}
3294
Devang Patel11cf3f42009-10-27 22:16:29 +00003295BinaryOperator *BinaryOperator::clone_impl() const {
3296 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003297}
3298
Devang Patel11cf3f42009-10-27 22:16:29 +00003299FCmpInst* FCmpInst::clone_impl() const {
3300 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003301}
3302
Devang Patel11cf3f42009-10-27 22:16:29 +00003303ICmpInst* ICmpInst::clone_impl() const {
3304 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003305}
3306
Devang Patel11cf3f42009-10-27 22:16:29 +00003307ExtractValueInst *ExtractValueInst::clone_impl() const {
3308 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003309}
3310
Devang Patel11cf3f42009-10-27 22:16:29 +00003311InsertValueInst *InsertValueInst::clone_impl() const {
3312 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003313}
3314
Devang Patel11cf3f42009-10-27 22:16:29 +00003315AllocaInst *AllocaInst::clone_impl() const {
3316 return new AllocaInst(getAllocatedType(),
3317 (Value*)getOperand(0),
3318 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003319}
3320
Devang Patel11cf3f42009-10-27 22:16:29 +00003321LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003322 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3323 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003324}
3325
Devang Patel11cf3f42009-10-27 22:16:29 +00003326StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003327 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003328 getAlignment(), getOrdering(), getSynchScope());
3329
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003330}
3331
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003332AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3333 AtomicCmpXchgInst *Result =
3334 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3335 getOrdering(), getSynchScope());
3336 Result->setVolatile(isVolatile());
3337 return Result;
3338}
3339
3340AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3341 AtomicRMWInst *Result =
3342 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3343 getOrdering(), getSynchScope());
3344 Result->setVolatile(isVolatile());
3345 return Result;
3346}
3347
Eli Friedmanfee02c62011-07-25 23:16:38 +00003348FenceInst *FenceInst::clone_impl() const {
3349 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3350}
3351
Devang Patel11cf3f42009-10-27 22:16:29 +00003352TruncInst *TruncInst::clone_impl() const {
3353 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003354}
3355
Devang Patel11cf3f42009-10-27 22:16:29 +00003356ZExtInst *ZExtInst::clone_impl() const {
3357 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003358}
3359
Devang Patel11cf3f42009-10-27 22:16:29 +00003360SExtInst *SExtInst::clone_impl() const {
3361 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003362}
3363
Devang Patel11cf3f42009-10-27 22:16:29 +00003364FPTruncInst *FPTruncInst::clone_impl() const {
3365 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003366}
3367
Devang Patel11cf3f42009-10-27 22:16:29 +00003368FPExtInst *FPExtInst::clone_impl() const {
3369 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003370}
3371
Devang Patel11cf3f42009-10-27 22:16:29 +00003372UIToFPInst *UIToFPInst::clone_impl() const {
3373 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003374}
3375
Devang Patel11cf3f42009-10-27 22:16:29 +00003376SIToFPInst *SIToFPInst::clone_impl() const {
3377 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003378}
3379
Devang Patel11cf3f42009-10-27 22:16:29 +00003380FPToUIInst *FPToUIInst::clone_impl() const {
3381 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003382}
3383
Devang Patel11cf3f42009-10-27 22:16:29 +00003384FPToSIInst *FPToSIInst::clone_impl() const {
3385 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003386}
3387
Devang Patel11cf3f42009-10-27 22:16:29 +00003388PtrToIntInst *PtrToIntInst::clone_impl() const {
3389 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003390}
3391
Devang Patel11cf3f42009-10-27 22:16:29 +00003392IntToPtrInst *IntToPtrInst::clone_impl() const {
3393 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003394}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003395
Devang Patel11cf3f42009-10-27 22:16:29 +00003396BitCastInst *BitCastInst::clone_impl() const {
3397 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003398}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003399
Devang Patel11cf3f42009-10-27 22:16:29 +00003400CallInst *CallInst::clone_impl() const {
3401 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003402}
3403
Devang Patel11cf3f42009-10-27 22:16:29 +00003404SelectInst *SelectInst::clone_impl() const {
3405 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003406}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003407
Devang Patel11cf3f42009-10-27 22:16:29 +00003408VAArgInst *VAArgInst::clone_impl() const {
3409 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003410}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003411
Devang Patel11cf3f42009-10-27 22:16:29 +00003412ExtractElementInst *ExtractElementInst::clone_impl() const {
3413 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003414}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003415
Devang Patel11cf3f42009-10-27 22:16:29 +00003416InsertElementInst *InsertElementInst::clone_impl() const {
3417 return InsertElementInst::Create(getOperand(0),
3418 getOperand(1),
3419 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003420}
3421
Devang Patel11cf3f42009-10-27 22:16:29 +00003422ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3423 return new ShuffleVectorInst(getOperand(0),
3424 getOperand(1),
3425 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003426}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003427
Devang Patel11cf3f42009-10-27 22:16:29 +00003428PHINode *PHINode::clone_impl() const {
3429 return new PHINode(*this);
3430}
3431
Bill Wendlingfae14752011-08-12 20:24:12 +00003432LandingPadInst *LandingPadInst::clone_impl() const {
3433 return new LandingPadInst(*this);
3434}
3435
Devang Patel11cf3f42009-10-27 22:16:29 +00003436ReturnInst *ReturnInst::clone_impl() const {
3437 return new(getNumOperands()) ReturnInst(*this);
3438}
3439
3440BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003441 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003442}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003443
Devang Patel11cf3f42009-10-27 22:16:29 +00003444SwitchInst *SwitchInst::clone_impl() const {
3445 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003446}
3447
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003448IndirectBrInst *IndirectBrInst::clone_impl() const {
3449 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003450}
3451
3452
Devang Patel11cf3f42009-10-27 22:16:29 +00003453InvokeInst *InvokeInst::clone_impl() const {
3454 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003455}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003456
Bill Wendlingf891bf82011-07-31 06:30:59 +00003457ResumeInst *ResumeInst::clone_impl() const {
3458 return new(1) ResumeInst(*this);
3459}
3460
Devang Patel11cf3f42009-10-27 22:16:29 +00003461UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003462 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003463 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003464}
3465
Devang Patel11cf3f42009-10-27 22:16:29 +00003466UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003467 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003468 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003469}