blob: 82f883ad98a34eca7040d3f5d843bb0841699212 [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:
Richard Trieua318b8d2011-09-21 03:09:09 +00002025 assert(0 && "Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026 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,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002062 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063 // Define the 144 possibilities for these two cast instructions. The values
2064 // in this matrix determine what to do in a given situation and select the
2065 // case in the switch below. The rows correspond to firstOp, the columns
2066 // correspond to secondOp. In looking at the table below, keep in mind
2067 // the following cast properties:
2068 //
2069 // Size Compare Source Destination
2070 // Operator Src ? Size Type Sign Type Sign
2071 // -------- ------------ ------------------- ---------------------
2072 // TRUNC > Integer Any Integral Any
2073 // ZEXT < Integral Unsigned Integer Any
2074 // SEXT < Integral Signed Integer Any
2075 // FPTOUI n/a FloatPt n/a Integral Unsigned
2076 // FPTOSI n/a FloatPt n/a Integral Signed
2077 // UITOFP n/a Integral Unsigned FloatPt n/a
2078 // SITOFP n/a Integral Signed FloatPt n/a
2079 // FPTRUNC > FloatPt n/a FloatPt n/a
2080 // FPEXT < FloatPt n/a FloatPt n/a
2081 // PTRTOINT n/a Pointer n/a Integral Unsigned
2082 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002083 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002084 //
2085 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002086 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2087 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002088 // of the produced value (we no longer know the top-part is all zeros).
2089 // Further this conversion is often much more expensive for typical hardware,
2090 // and causes issues when building libgcc. We disallow fptosi+sext for the
2091 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092 const unsigned numCastOps =
2093 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2094 static const uint8_t CastResults[numCastOps][numCastOps] = {
2095 // T F F U S F F P I B -+
2096 // R Z S P P I I T P 2 N T |
2097 // U E E 2 2 2 2 R E I T C +- secondOp
2098 // N X X U S F F N X N 2 V |
2099 // C T T I I P P C T T P T -+
2100 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2101 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2102 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002103 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2104 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002105 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2106 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2107 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2108 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2109 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2110 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2111 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2112 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002113
2114 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002115 // merging. However, bitcast of A->B->A are allowed.
2116 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2117 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2118 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2119
2120 // Check if any of the bitcasts convert scalars<->vectors.
2121 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2122 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2123 // Unless we are bitcasing to the original type, disallow optimizations.
2124 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002125
2126 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2127 [secondOp-Instruction::CastOpsBegin];
2128 switch (ElimCase) {
2129 case 0:
2130 // categorically disallowed
2131 return 0;
2132 case 1:
2133 // allowed, use first cast's opcode
2134 return firstOp;
2135 case 2:
2136 // allowed, use second cast's opcode
2137 return secondOp;
2138 case 3:
2139 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002140 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002141 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002142 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002143 return firstOp;
2144 return 0;
2145 case 4:
2146 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002147 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002148 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002149 return firstOp;
2150 return 0;
2151 case 5:
2152 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002153 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002154 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002155 return secondOp;
2156 return 0;
2157 case 6:
2158 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002159 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002160 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161 return secondOp;
2162 return 0;
2163 case 7: {
2164 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002165 if (!IntPtrTy)
2166 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002167 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2168 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002169 if (MidSize >= PtrSize)
2170 return Instruction::BitCast;
2171 return 0;
2172 }
2173 case 8: {
2174 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2175 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2176 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002177 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2178 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002179 if (SrcSize == DstSize)
2180 return Instruction::BitCast;
2181 else if (SrcSize < DstSize)
2182 return firstOp;
2183 return secondOp;
2184 }
2185 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2186 return Instruction::ZExt;
2187 case 10:
2188 // fpext followed by ftrunc is allowed if the bit size returned to is
2189 // the same as the original, in which case its just a bitcast
2190 if (SrcTy == DstTy)
2191 return Instruction::BitCast;
2192 return 0; // If the types are not the same we can't eliminate it.
2193 case 11:
2194 // bitcast followed by ptrtoint is allowed as long as the bitcast
2195 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002196 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002197 return secondOp;
2198 return 0;
2199 case 12:
2200 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002201 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002202 return firstOp;
2203 return 0;
2204 case 13: {
2205 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002206 if (!IntPtrTy)
2207 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002208 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2209 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2210 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211 if (SrcSize <= PtrSize && SrcSize == DstSize)
2212 return Instruction::BitCast;
2213 return 0;
2214 }
2215 case 99:
2216 // cast combination can't happen (error in input). This is for all cases
2217 // where the MidTy is not the same for the two cast instructions.
Richard Trieua318b8d2011-09-21 03:09:09 +00002218 assert(0 && "Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219 return 0;
2220 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002221 assert(0 && "Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222 return 0;
2223 }
2224 return 0;
2225}
2226
Chris Lattner229907c2011-07-18 04:54:35 +00002227CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002228 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002229 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002230 // Construct and return the appropriate CastInst subclass
2231 switch (op) {
2232 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2233 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2234 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2235 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2236 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2237 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2238 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2239 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2240 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2241 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2242 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2243 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2244 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002245 assert(0 && "Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002246 }
2247 return 0;
2248}
2249
Chris Lattner229907c2011-07-18 04:54:35 +00002250CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002251 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002252 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002253 // Construct and return the appropriate CastInst subclass
2254 switch (op) {
2255 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2256 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2257 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2258 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2259 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2260 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2261 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2262 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2263 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2264 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2265 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2266 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2267 default:
Richard Trieua318b8d2011-09-21 03:09:09 +00002268 assert(0 && "Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002269 }
2270 return 0;
2271}
2272
Chris Lattner229907c2011-07-18 04:54:35 +00002273CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002274 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002275 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002276 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002277 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2278 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002279}
2280
Chris Lattner229907c2011-07-18 04:54:35 +00002281CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002282 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002283 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002284 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002285 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2286 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002287}
2288
Chris Lattner229907c2011-07-18 04:54:35 +00002289CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002290 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002291 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002292 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002293 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2294 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002295}
2296
Chris Lattner229907c2011-07-18 04:54:35 +00002297CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002298 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002299 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002300 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002301 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2302 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002303}
2304
Chris Lattner229907c2011-07-18 04:54:35 +00002305CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002306 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002307 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002308 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002309 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2310 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002311}
2312
Chris Lattner229907c2011-07-18 04:54:35 +00002313CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002314 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002315 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002316 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002317 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2318 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002319}
2320
Chris Lattner229907c2011-07-18 04:54:35 +00002321CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002322 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002323 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002324 assert(S->getType()->isPointerTy() && "Invalid cast");
2325 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002326 "Invalid cast");
2327
Duncan Sands9dff9be2010-02-15 16:12:20 +00002328 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002329 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2330 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002331}
2332
2333/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002334CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002335 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002336 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002337 assert(S->getType()->isPointerTy() && "Invalid cast");
2338 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002339 "Invalid cast");
2340
Duncan Sands9dff9be2010-02-15 16:12:20 +00002341 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002342 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2343 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002344}
2345
Chris Lattner229907c2011-07-18 04:54:35 +00002346CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002347 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002348 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002349 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002350 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002351 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2352 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002353 Instruction::CastOps opcode =
2354 (SrcBits == DstBits ? Instruction::BitCast :
2355 (SrcBits > DstBits ? Instruction::Trunc :
2356 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002357 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002358}
2359
Chris Lattner229907c2011-07-18 04:54:35 +00002360CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002361 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002362 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002363 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002364 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002365 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2366 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002367 Instruction::CastOps opcode =
2368 (SrcBits == DstBits ? Instruction::BitCast :
2369 (SrcBits > DstBits ? Instruction::Trunc :
2370 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002371 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002372}
2373
Chris Lattner229907c2011-07-18 04:54:35 +00002374CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002375 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002376 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002377 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002378 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002379 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2380 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002381 Instruction::CastOps opcode =
2382 (SrcBits == DstBits ? Instruction::BitCast :
2383 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002384 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002385}
2386
Chris Lattner229907c2011-07-18 04:54:35 +00002387CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002388 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002389 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002390 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002391 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002392 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2393 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002394 Instruction::CastOps opcode =
2395 (SrcBits == DstBits ? Instruction::BitCast :
2396 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002397 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002398}
2399
Duncan Sands55e50902008-01-06 10:12:28 +00002400// Check whether it is valid to call getCastOpcode for these types.
2401// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002402bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002403 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2404 return false;
2405
2406 if (SrcTy == DestTy)
2407 return true;
2408
Chris Lattner229907c2011-07-18 04:54:35 +00002409 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2410 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002411 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2412 // An element by element cast. Valid if casting the elements is valid.
2413 SrcTy = SrcVecTy->getElementType();
2414 DestTy = DestVecTy->getElementType();
2415 }
2416
Duncan Sands55e50902008-01-06 10:12:28 +00002417 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002418 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2419 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002420
2421 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002422 if (DestTy->isIntegerTy()) { // Casting to integral
2423 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002424 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002425 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002426 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002427 } else if (SrcTy->isVectorTy()) { // Casting from vector
2428 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002429 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002430 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002431 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002432 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2433 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002434 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002435 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002436 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002437 } else if (SrcTy->isVectorTy()) { // Casting from vector
2438 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002439 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002440 return false;
2441 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002442 } else if (DestTy->isVectorTy()) { // Casting to vector
2443 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002444 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002445 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002446 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002447 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002448 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002449 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002450 return false;
2451 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002452 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002453 if (SrcTy->isVectorTy()) {
2454 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002455 } else {
2456 return false;
2457 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002458 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002459 return false;
2460 }
2461}
2462
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463// Provide a way to get a "cast" where the cast opcode is inferred from the
2464// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002465// logic in the castIsValid function below. This axiom should hold:
2466// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2467// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002469// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002470Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002471CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002472 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2473 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474
Duncan Sands55e50902008-01-06 10:12:28 +00002475 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2476 "Only first class types are castable!");
2477
Duncan Sandsa8514532011-05-18 07:13:41 +00002478 if (SrcTy == DestTy)
2479 return BitCast;
2480
Chris Lattner229907c2011-07-18 04:54:35 +00002481 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2482 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002483 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2484 // An element by element cast. Find the appropriate opcode based on the
2485 // element types.
2486 SrcTy = SrcVecTy->getElementType();
2487 DestTy = DestVecTy->getElementType();
2488 }
2489
2490 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002491 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2492 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002493
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002495 if (DestTy->isIntegerTy()) { // Casting to integral
2496 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497 if (DestBits < SrcBits)
2498 return Trunc; // int -> smaller int
2499 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002500 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501 return SExt; // signed -> SEXT
2502 else
2503 return ZExt; // unsigned -> ZEXT
2504 } else {
2505 return BitCast; // Same size, No-op cast
2506 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002507 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002508 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509 return FPToSI; // FP -> sint
2510 else
2511 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002512 } else if (SrcTy->isVectorTy()) {
2513 assert(DestBits == SrcBits &&
2514 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515 return BitCast; // Same size, no-op cast
2516 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002517 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518 "Casting from a value that is not first-class type");
2519 return PtrToInt; // ptr -> int
2520 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002521 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2522 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002523 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524 return SIToFP; // sint -> FP
2525 else
2526 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002527 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528 if (DestBits < SrcBits) {
2529 return FPTrunc; // FP -> smaller FP
2530 } else if (DestBits > SrcBits) {
2531 return FPExt; // FP -> larger FP
2532 } else {
2533 return BitCast; // same size, no-op cast
2534 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002535 } else if (SrcTy->isVectorTy()) {
2536 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002537 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002538 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002540 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002542 } else if (DestTy->isVectorTy()) {
2543 assert(DestBits == SrcBits &&
2544 "Illegal cast to vector (wrong type or size)");
2545 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002546 } else if (DestTy->isPointerTy()) {
2547 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002548 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002549 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550 return IntToPtr; // int -> ptr
2551 } else {
Richard Trieua318b8d2011-09-21 03:09:09 +00002552 assert(0 && "Casting pointer to other than pointer or int");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002554 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002555 if (SrcTy->isVectorTy()) {
2556 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002557 return BitCast; // 64-bit vector to MMX
2558 } else {
Richard Trieua318b8d2011-09-21 03:09:09 +00002559 assert(0 && "Illegal cast to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002560 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561 } else {
Richard Trieua318b8d2011-09-21 03:09:09 +00002562 assert(0 && "Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563 }
2564
2565 // If we fall through to here we probably hit an assertion cast above
2566 // and assertions are not turned on. Anything we return is an error, so
2567 // BitCast is as good a choice as any.
2568 return BitCast;
2569}
2570
2571//===----------------------------------------------------------------------===//
2572// CastInst SubClass Constructors
2573//===----------------------------------------------------------------------===//
2574
2575/// Check that the construction parameters for a CastInst are correct. This
2576/// could be broken out into the separate constructors but it is useful to have
2577/// it in one place and to eliminate the redundant code for getting the sizes
2578/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002579bool
Chris Lattner229907c2011-07-18 04:54:35 +00002580CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581
2582 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002583 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002584 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2585 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586 return false;
2587
2588 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002589 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2590 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591
Duncan Sands7f646562011-05-18 09:21:57 +00002592 // If these are vector types, get the lengths of the vectors (using zero for
2593 // scalar types means that checking that vector lengths match also checks that
2594 // scalars are not being converted to vectors or vectors to scalars).
2595 unsigned SrcLength = SrcTy->isVectorTy() ?
2596 cast<VectorType>(SrcTy)->getNumElements() : 0;
2597 unsigned DstLength = DstTy->isVectorTy() ?
2598 cast<VectorType>(DstTy)->getNumElements() : 0;
2599
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600 // Switch on the opcode provided
2601 switch (op) {
2602 default: return false; // This is an input error
2603 case Instruction::Trunc:
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::ZExt:
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::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002610 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2611 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612 case Instruction::FPTrunc:
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::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002616 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2617 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002620 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2621 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002624 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2625 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002627 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002629 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630 case Instruction::BitCast:
2631 // BitCast implies a no-op cast of type only. No bits change.
2632 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002633 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634 return false;
2635
Duncan Sands55e50902008-01-06 10:12:28 +00002636 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637 // these cases, the cast is okay if the source and destination bit widths
2638 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002639 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002640 }
2641}
2642
2643TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002644 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002645) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002646 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647}
2648
2649TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002650 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002651) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002652 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653}
2654
2655ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002656 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002658 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659}
2660
2661ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002662 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002663) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002664 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665}
2666SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002667 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002668) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002669 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002670}
2671
Jeff Cohencc08c832006-12-02 02:22:01 +00002672SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002673 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002675 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002676}
2677
2678FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002679 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002680) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002681 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682}
2683
2684FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002685 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002686) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002687 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002688}
2689
2690FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002691 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002692) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002693 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002694}
2695
2696FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002697 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002698) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002699 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002700}
2701
2702UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002703 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002704) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002705 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002706}
2707
2708UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002709 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002710) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002711 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002712}
2713
2714SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002715 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002716) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002717 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002718}
2719
2720SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002721 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002722) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002723 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002724}
2725
2726FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002727 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002728) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002729 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002730}
2731
2732FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002733 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002734) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002735 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002736}
2737
2738FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002739 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002740) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002741 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002742}
2743
2744FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002745 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002746) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002747 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002748}
2749
2750PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002751 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002752) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002753 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002754}
2755
2756PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002757 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002758) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002759 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002760}
2761
2762IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002763 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002764) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002765 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002766}
2767
2768IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002769 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002770) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002771 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002772}
2773
2774BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002775 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002776) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002777 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002778}
2779
2780BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002781 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002782) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002783 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002784}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002785
2786//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002787// CmpInst Classes
2788//===----------------------------------------------------------------------===//
2789
Chris Lattneraec33da2010-01-22 06:25:37 +00002790void CmpInst::Anchor() const {}
2791
Chris Lattner229907c2011-07-18 04:54:35 +00002792CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002793 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002794 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002795 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002796 OperandTraits<CmpInst>::op_begin(this),
2797 OperandTraits<CmpInst>::operands(this),
2798 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002799 Op<0>() = LHS;
2800 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002801 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002802 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002803}
Gabor Greiff6caff662008-05-10 08:32:32 +00002804
Chris Lattner229907c2011-07-18 04:54:35 +00002805CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002806 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002807 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002808 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002809 OperandTraits<CmpInst>::op_begin(this),
2810 OperandTraits<CmpInst>::operands(this),
2811 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002812 Op<0>() = LHS;
2813 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002814 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002815 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002816}
2817
2818CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002819CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002820 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002821 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002822 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002823 if (InsertBefore)
2824 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2825 S1, S2, Name);
2826 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002827 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002828 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002829 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002830
2831 if (InsertBefore)
2832 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2833 S1, S2, Name);
2834 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002835 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002836 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002837}
2838
2839CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002840CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002841 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002842 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002843 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2844 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002845 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002846 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2847 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002848}
2849
2850void CmpInst::swapOperands() {
2851 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2852 IC->swapOperands();
2853 else
2854 cast<FCmpInst>(this)->swapOperands();
2855}
2856
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002857bool CmpInst::isCommutative() const {
2858 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002859 return IC->isCommutative();
2860 return cast<FCmpInst>(this)->isCommutative();
2861}
2862
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002863bool CmpInst::isEquality() const {
2864 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002865 return IC->isEquality();
2866 return cast<FCmpInst>(this)->isEquality();
2867}
2868
2869
Dan Gohman4e724382008-05-31 02:47:54 +00002870CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002871 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00002872 default: assert(0 && "Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002873 case ICMP_EQ: return ICMP_NE;
2874 case ICMP_NE: return ICMP_EQ;
2875 case ICMP_UGT: return ICMP_ULE;
2876 case ICMP_ULT: return ICMP_UGE;
2877 case ICMP_UGE: return ICMP_ULT;
2878 case ICMP_ULE: return ICMP_UGT;
2879 case ICMP_SGT: return ICMP_SLE;
2880 case ICMP_SLT: return ICMP_SGE;
2881 case ICMP_SGE: return ICMP_SLT;
2882 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002883
Dan Gohman4e724382008-05-31 02:47:54 +00002884 case FCMP_OEQ: return FCMP_UNE;
2885 case FCMP_ONE: return FCMP_UEQ;
2886 case FCMP_OGT: return FCMP_ULE;
2887 case FCMP_OLT: return FCMP_UGE;
2888 case FCMP_OGE: return FCMP_ULT;
2889 case FCMP_OLE: return FCMP_UGT;
2890 case FCMP_UEQ: return FCMP_ONE;
2891 case FCMP_UNE: return FCMP_OEQ;
2892 case FCMP_UGT: return FCMP_OLE;
2893 case FCMP_ULT: return FCMP_OGE;
2894 case FCMP_UGE: return FCMP_OLT;
2895 case FCMP_ULE: return FCMP_OGT;
2896 case FCMP_ORD: return FCMP_UNO;
2897 case FCMP_UNO: return FCMP_ORD;
2898 case FCMP_TRUE: return FCMP_FALSE;
2899 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002900 }
2901}
2902
Reid Spencer266e42b2006-12-23 06:05:41 +00002903ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2904 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00002905 default: assert(0 && "Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002906 case ICMP_EQ: case ICMP_NE:
2907 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2908 return pred;
2909 case ICMP_UGT: return ICMP_SGT;
2910 case ICMP_ULT: return ICMP_SLT;
2911 case ICMP_UGE: return ICMP_SGE;
2912 case ICMP_ULE: return ICMP_SLE;
2913 }
2914}
2915
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002916ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2917 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00002918 default: assert(0 && "Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002919 case ICMP_EQ: case ICMP_NE:
2920 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2921 return pred;
2922 case ICMP_SGT: return ICMP_UGT;
2923 case ICMP_SLT: return ICMP_ULT;
2924 case ICMP_SGE: return ICMP_UGE;
2925 case ICMP_SLE: return ICMP_ULE;
2926 }
2927}
2928
Reid Spencer0286bc12007-02-28 22:00:54 +00002929/// Initialize a set of values that all satisfy the condition with C.
2930///
2931ConstantRange
2932ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2933 APInt Lower(C);
2934 APInt Upper(C);
2935 uint32_t BitWidth = C.getBitWidth();
2936 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002937 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002938 case ICmpInst::ICMP_EQ: Upper++; break;
2939 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002940 case ICmpInst::ICMP_ULT:
2941 Lower = APInt::getMinValue(BitWidth);
2942 // Check for an empty-set condition.
2943 if (Lower == Upper)
2944 return ConstantRange(BitWidth, /*isFullSet=*/false);
2945 break;
2946 case ICmpInst::ICMP_SLT:
2947 Lower = APInt::getSignedMinValue(BitWidth);
2948 // Check for an empty-set condition.
2949 if (Lower == Upper)
2950 return ConstantRange(BitWidth, /*isFullSet=*/false);
2951 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002952 case ICmpInst::ICMP_UGT:
2953 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002954 // Check for an empty-set condition.
2955 if (Lower == Upper)
2956 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002957 break;
2958 case ICmpInst::ICMP_SGT:
2959 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002960 // Check for an empty-set condition.
2961 if (Lower == Upper)
2962 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002963 break;
2964 case ICmpInst::ICMP_ULE:
2965 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002966 // Check for a full-set condition.
2967 if (Lower == Upper)
2968 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002969 break;
2970 case ICmpInst::ICMP_SLE:
2971 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002972 // Check for a full-set condition.
2973 if (Lower == Upper)
2974 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002975 break;
2976 case ICmpInst::ICMP_UGE:
2977 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002978 // Check for a full-set condition.
2979 if (Lower == Upper)
2980 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002981 break;
2982 case ICmpInst::ICMP_SGE:
2983 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002984 // Check for a full-set condition.
2985 if (Lower == Upper)
2986 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002987 break;
2988 }
2989 return ConstantRange(Lower, Upper);
2990}
2991
Dan Gohman4e724382008-05-31 02:47:54 +00002992CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002993 switch (pred) {
Richard Trieua318b8d2011-09-21 03:09:09 +00002994 default: assert(0 && "Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00002995 case ICMP_EQ: case ICMP_NE:
2996 return pred;
2997 case ICMP_SGT: return ICMP_SLT;
2998 case ICMP_SLT: return ICMP_SGT;
2999 case ICMP_SGE: return ICMP_SLE;
3000 case ICMP_SLE: return ICMP_SGE;
3001 case ICMP_UGT: return ICMP_ULT;
3002 case ICMP_ULT: return ICMP_UGT;
3003 case ICMP_UGE: return ICMP_ULE;
3004 case ICMP_ULE: return ICMP_UGE;
3005
Reid Spencerd9436b62006-11-20 01:22:35 +00003006 case FCMP_FALSE: case FCMP_TRUE:
3007 case FCMP_OEQ: case FCMP_ONE:
3008 case FCMP_UEQ: case FCMP_UNE:
3009 case FCMP_ORD: case FCMP_UNO:
3010 return pred;
3011 case FCMP_OGT: return FCMP_OLT;
3012 case FCMP_OLT: return FCMP_OGT;
3013 case FCMP_OGE: return FCMP_OLE;
3014 case FCMP_OLE: return FCMP_OGE;
3015 case FCMP_UGT: return FCMP_ULT;
3016 case FCMP_ULT: return FCMP_UGT;
3017 case FCMP_UGE: return FCMP_ULE;
3018 case FCMP_ULE: return FCMP_UGE;
3019 }
3020}
3021
Reid Spencer266e42b2006-12-23 06:05:41 +00003022bool CmpInst::isUnsigned(unsigned short predicate) {
3023 switch (predicate) {
3024 default: return false;
3025 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3026 case ICmpInst::ICMP_UGE: return true;
3027 }
3028}
3029
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003030bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003031 switch (predicate) {
3032 default: return false;
3033 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3034 case ICmpInst::ICMP_SGE: return true;
3035 }
3036}
3037
3038bool CmpInst::isOrdered(unsigned short predicate) {
3039 switch (predicate) {
3040 default: return false;
3041 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3042 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3043 case FCmpInst::FCMP_ORD: return true;
3044 }
3045}
3046
3047bool CmpInst::isUnordered(unsigned short predicate) {
3048 switch (predicate) {
3049 default: return false;
3050 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3051 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3052 case FCmpInst::FCMP_UNO: return true;
3053 }
3054}
3055
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003056bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3057 switch(predicate) {
3058 default: return false;
3059 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3060 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3061 }
3062}
3063
3064bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3065 switch(predicate) {
3066 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3067 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3068 default: return false;
3069 }
3070}
3071
3072
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003073//===----------------------------------------------------------------------===//
3074// SwitchInst Implementation
3075//===----------------------------------------------------------------------===//
3076
Chris Lattnerbaf00152010-11-17 05:41:46 +00003077void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3078 assert(Value && Default && NumReserved);
3079 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003080 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003081 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003082
Gabor Greif2d3024d2008-05-26 21:33:52 +00003083 OperandList[0] = Value;
3084 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003085}
3086
Chris Lattner2195fc42007-02-24 00:55:48 +00003087/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3088/// switch on and a default destination. The number of additional cases can
3089/// be specified here to make memory allocation more efficient. This
3090/// constructor can also autoinsert before another instruction.
3091SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3092 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003093 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3094 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003095 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003096}
3097
3098/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3099/// switch on and a default destination. The number of additional cases can
3100/// be specified here to make memory allocation more efficient. This
3101/// constructor also autoinserts at the end of the specified BasicBlock.
3102SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3103 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003104 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3105 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003106 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003107}
3108
Misha Brukmanb1c93172005-04-21 23:48:37 +00003109SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003110 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3111 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3112 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003113 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003114 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003115 OL[i] = InOL[i];
3116 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003117 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003118 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003119}
3120
Gordon Henriksen14a55692007-12-10 02:14:30 +00003121SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003122 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003123}
3124
3125
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003126/// addCase - Add an entry to the switch instruction...
3127///
Chris Lattner47ac1872005-02-24 05:32:09 +00003128void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003129 unsigned OpNo = NumOperands;
3130 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003131 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003132 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003133 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003134 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003135 OperandList[OpNo] = OnVal;
3136 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003137}
3138
3139/// removeCase - This method removes the specified successor from the switch
3140/// instruction. Note that this cannot be used to remove the default
3141/// destination (successor #0).
3142///
3143void SwitchInst::removeCase(unsigned idx) {
3144 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003145 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3146
3147 unsigned NumOps = getNumOperands();
3148 Use *OL = OperandList;
3149
Jay Foad14277722011-02-01 09:22:34 +00003150 // Overwrite this case with the end of the list.
3151 if ((idx + 1) * 2 != NumOps) {
3152 OL[idx * 2] = OL[NumOps - 2];
3153 OL[idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003154 }
3155
3156 // Nuke the last value.
3157 OL[NumOps-2].set(0);
3158 OL[NumOps-2+1].set(0);
3159 NumOperands = NumOps-2;
3160}
3161
Jay Foade98f29d2011-04-01 08:00:58 +00003162/// growOperands - grow operands - This grows the operand list in response
3163/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003164///
Jay Foade98f29d2011-04-01 08:00:58 +00003165void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003166 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003167 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003168
3169 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003170 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003171 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003172 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003173 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003174 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003175 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003176 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003177}
3178
3179
3180BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3181 return getSuccessor(idx);
3182}
3183unsigned SwitchInst::getNumSuccessorsV() const {
3184 return getNumSuccessors();
3185}
3186void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3187 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003188}
Chris Lattnerf22be932004-10-15 23:52:53 +00003189
Chris Lattner3ed871f2009-10-27 19:13:16 +00003190//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003191// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003192//===----------------------------------------------------------------------===//
3193
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003194void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003195 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003196 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003197 ReservedSpace = 1+NumDests;
3198 NumOperands = 1;
3199 OperandList = allocHungoffUses(ReservedSpace);
3200
3201 OperandList[0] = Address;
3202}
3203
3204
Jay Foade98f29d2011-04-01 08:00:58 +00003205/// growOperands - grow operands - This grows the operand list in response
3206/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003207///
Jay Foade98f29d2011-04-01 08:00:58 +00003208void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003209 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003210 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003211
3212 ReservedSpace = NumOps;
3213 Use *NewOps = allocHungoffUses(NumOps);
3214 Use *OldOps = OperandList;
3215 for (unsigned i = 0; i != e; ++i)
3216 NewOps[i] = OldOps[i];
3217 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003218 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003219}
3220
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003221IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3222 Instruction *InsertBefore)
3223: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003224 0, 0, InsertBefore) {
3225 init(Address, NumCases);
3226}
3227
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003228IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3229 BasicBlock *InsertAtEnd)
3230: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003231 0, 0, InsertAtEnd) {
3232 init(Address, NumCases);
3233}
3234
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003235IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3236 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003237 allocHungoffUses(IBI.getNumOperands()),
3238 IBI.getNumOperands()) {
3239 Use *OL = OperandList, *InOL = IBI.OperandList;
3240 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3241 OL[i] = InOL[i];
3242 SubclassOptionalData = IBI.SubclassOptionalData;
3243}
3244
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003245IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003246 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003247}
3248
3249/// addDestination - Add a destination.
3250///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003251void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003252 unsigned OpNo = NumOperands;
3253 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003254 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003255 // Initialize some new operands.
3256 assert(OpNo < ReservedSpace && "Growing didn't work!");
3257 NumOperands = OpNo+1;
3258 OperandList[OpNo] = DestBB;
3259}
3260
3261/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003262/// indirectbr instruction.
3263void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003264 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3265
3266 unsigned NumOps = getNumOperands();
3267 Use *OL = OperandList;
3268
3269 // Replace this value with the last one.
3270 OL[idx+1] = OL[NumOps-1];
3271
3272 // Nuke the last value.
3273 OL[NumOps-1].set(0);
3274 NumOperands = NumOps-1;
3275}
3276
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003277BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003278 return getSuccessor(idx);
3279}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003280unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003281 return getNumSuccessors();
3282}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003283void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003284 setSuccessor(idx, B);
3285}
3286
3287//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003288// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003289//===----------------------------------------------------------------------===//
3290
Chris Lattnerf22be932004-10-15 23:52:53 +00003291// Define these methods here so vtables don't get emitted into every translation
3292// unit that uses these classes.
3293
Devang Patel11cf3f42009-10-27 22:16:29 +00003294GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3295 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003296}
3297
Devang Patel11cf3f42009-10-27 22:16:29 +00003298BinaryOperator *BinaryOperator::clone_impl() const {
3299 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003300}
3301
Devang Patel11cf3f42009-10-27 22:16:29 +00003302FCmpInst* FCmpInst::clone_impl() const {
3303 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003304}
3305
Devang Patel11cf3f42009-10-27 22:16:29 +00003306ICmpInst* ICmpInst::clone_impl() const {
3307 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003308}
3309
Devang Patel11cf3f42009-10-27 22:16:29 +00003310ExtractValueInst *ExtractValueInst::clone_impl() const {
3311 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003312}
3313
Devang Patel11cf3f42009-10-27 22:16:29 +00003314InsertValueInst *InsertValueInst::clone_impl() const {
3315 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003316}
3317
Devang Patel11cf3f42009-10-27 22:16:29 +00003318AllocaInst *AllocaInst::clone_impl() const {
3319 return new AllocaInst(getAllocatedType(),
3320 (Value*)getOperand(0),
3321 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003322}
3323
Devang Patel11cf3f42009-10-27 22:16:29 +00003324LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003325 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3326 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003327}
3328
Devang Patel11cf3f42009-10-27 22:16:29 +00003329StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003330 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003331 getAlignment(), getOrdering(), getSynchScope());
3332
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003333}
3334
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003335AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3336 AtomicCmpXchgInst *Result =
3337 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3338 getOrdering(), getSynchScope());
3339 Result->setVolatile(isVolatile());
3340 return Result;
3341}
3342
3343AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3344 AtomicRMWInst *Result =
3345 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3346 getOrdering(), getSynchScope());
3347 Result->setVolatile(isVolatile());
3348 return Result;
3349}
3350
Eli Friedmanfee02c62011-07-25 23:16:38 +00003351FenceInst *FenceInst::clone_impl() const {
3352 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3353}
3354
Devang Patel11cf3f42009-10-27 22:16:29 +00003355TruncInst *TruncInst::clone_impl() const {
3356 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003357}
3358
Devang Patel11cf3f42009-10-27 22:16:29 +00003359ZExtInst *ZExtInst::clone_impl() const {
3360 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003361}
3362
Devang Patel11cf3f42009-10-27 22:16:29 +00003363SExtInst *SExtInst::clone_impl() const {
3364 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003365}
3366
Devang Patel11cf3f42009-10-27 22:16:29 +00003367FPTruncInst *FPTruncInst::clone_impl() const {
3368 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003369}
3370
Devang Patel11cf3f42009-10-27 22:16:29 +00003371FPExtInst *FPExtInst::clone_impl() const {
3372 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003373}
3374
Devang Patel11cf3f42009-10-27 22:16:29 +00003375UIToFPInst *UIToFPInst::clone_impl() const {
3376 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003377}
3378
Devang Patel11cf3f42009-10-27 22:16:29 +00003379SIToFPInst *SIToFPInst::clone_impl() const {
3380 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003381}
3382
Devang Patel11cf3f42009-10-27 22:16:29 +00003383FPToUIInst *FPToUIInst::clone_impl() const {
3384 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003385}
3386
Devang Patel11cf3f42009-10-27 22:16:29 +00003387FPToSIInst *FPToSIInst::clone_impl() const {
3388 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003389}
3390
Devang Patel11cf3f42009-10-27 22:16:29 +00003391PtrToIntInst *PtrToIntInst::clone_impl() const {
3392 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003393}
3394
Devang Patel11cf3f42009-10-27 22:16:29 +00003395IntToPtrInst *IntToPtrInst::clone_impl() const {
3396 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003397}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003398
Devang Patel11cf3f42009-10-27 22:16:29 +00003399BitCastInst *BitCastInst::clone_impl() const {
3400 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003401}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003402
Devang Patel11cf3f42009-10-27 22:16:29 +00003403CallInst *CallInst::clone_impl() const {
3404 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003405}
3406
Devang Patel11cf3f42009-10-27 22:16:29 +00003407SelectInst *SelectInst::clone_impl() const {
3408 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003409}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003410
Devang Patel11cf3f42009-10-27 22:16:29 +00003411VAArgInst *VAArgInst::clone_impl() const {
3412 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003413}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003414
Devang Patel11cf3f42009-10-27 22:16:29 +00003415ExtractElementInst *ExtractElementInst::clone_impl() const {
3416 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003417}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003418
Devang Patel11cf3f42009-10-27 22:16:29 +00003419InsertElementInst *InsertElementInst::clone_impl() const {
3420 return InsertElementInst::Create(getOperand(0),
3421 getOperand(1),
3422 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003423}
3424
Devang Patel11cf3f42009-10-27 22:16:29 +00003425ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3426 return new ShuffleVectorInst(getOperand(0),
3427 getOperand(1),
3428 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003429}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003430
Devang Patel11cf3f42009-10-27 22:16:29 +00003431PHINode *PHINode::clone_impl() const {
3432 return new PHINode(*this);
3433}
3434
Bill Wendlingfae14752011-08-12 20:24:12 +00003435LandingPadInst *LandingPadInst::clone_impl() const {
3436 return new LandingPadInst(*this);
3437}
3438
Devang Patel11cf3f42009-10-27 22:16:29 +00003439ReturnInst *ReturnInst::clone_impl() const {
3440 return new(getNumOperands()) ReturnInst(*this);
3441}
3442
3443BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003444 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003445}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003446
Devang Patel11cf3f42009-10-27 22:16:29 +00003447SwitchInst *SwitchInst::clone_impl() const {
3448 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003449}
3450
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003451IndirectBrInst *IndirectBrInst::clone_impl() const {
3452 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003453}
3454
3455
Devang Patel11cf3f42009-10-27 22:16:29 +00003456InvokeInst *InvokeInst::clone_impl() const {
3457 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003458}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003459
Bill Wendlingf891bf82011-07-31 06:30:59 +00003460ResumeInst *ResumeInst::clone_impl() const {
3461 return new(1) ResumeInst(*this);
3462}
3463
Devang Patel11cf3f42009-10-27 22:16:29 +00003464UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003465 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003466 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003467}
3468
Devang Patel11cf3f42009-10-27 22:16:29 +00003469UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003470 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003471 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003472}