blob: d5b756dac09bf44df96f3e83c30979dd8a1ef07b [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000032User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
34 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000035 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000036 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037}
38
Gordon Henriksen14a55692007-12-10 02:14:30 +000039//===----------------------------------------------------------------------===//
40// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43// Out of line virtual method, so the vtable, etc has a home.
44TerminatorInst::~TerminatorInst() {
45}
46
Gabor Greiff6caff662008-05-10 08:32:32 +000047//===----------------------------------------------------------------------===//
48// UnaryInstruction Class
49//===----------------------------------------------------------------------===//
50
Gordon Henriksen14a55692007-12-10 02:14:30 +000051// Out of line virtual method, so the vtable, etc has a home.
52UnaryInstruction::~UnaryInstruction() {
53}
54
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000056// SelectInst Class
57//===----------------------------------------------------------------------===//
58
59/// areInvalidOperands - Return a string if the specified operands are invalid
60/// for a select operation, otherwise return null.
61const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
64
Chris Lattner229907c2011-07-18 04:54:35 +000065 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000066 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000067 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000068 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000069 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Chris Lattner88107952008-12-29 00:12:50 +000070 if (ET == 0)
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000075 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000076 return "select condition must be i1 or <n x i1>";
77 }
78 return 0;
79}
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000083// PHINode Class
84//===----------------------------------------------------------------------===//
85
86PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000089 ReservedSpace(PN.getNumOperands()) {
Jay Foad61ea0e42011-06-23 09:09:15 +000090 std::copy(PN.op_begin(), PN.op_end(), op_begin());
91 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000092 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000093}
94
Gordon Henriksen14a55692007-12-10 02:14:30 +000095PHINode::~PHINode() {
Jay Foadbbb91f22011-01-16 15:30:52 +000096 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097}
98
Jay Foad61ea0e42011-06-23 09:09:15 +000099Use *PHINode::allocHungoffUses(unsigned N) const {
100 // Allocate the array of Uses of the incoming values, followed by a pointer
101 // (with bottom bit set) to the User, followed by the array of pointers to
102 // the incoming basic blocks.
103 size_t size = N * sizeof(Use) + sizeof(Use::UserRef)
104 + N * sizeof(BasicBlock*);
105 Use *Begin = static_cast<Use*>(::operator new(size));
106 Use *End = Begin + N;
107 (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1);
108 return Use::initTags(Begin, End);
109}
110
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000111// removeIncomingValue - Remove an incoming value. This is useful if a
112// predecessor basic block is deleted.
113Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000114 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115
116 // Move everything after this operand down.
117 //
118 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000121 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
122 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000123
124 // Nuke the last value.
Jay Foad61ea0e42011-06-23 09:09:15 +0000125 Op<-1>().set(0);
126 --NumOperands;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000127
128 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000129 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000131 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000132 eraseFromParent();
133 }
134 return Removed;
135}
136
Jay Foade98f29d2011-04-01 08:00:58 +0000137/// growOperands - grow operands - This grows the operand list in response
138/// to a push_back style of operation. This grows the number of ops by 1.5
139/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000140///
Jay Foade98f29d2011-04-01 08:00:58 +0000141void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000142 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000143 unsigned NumOps = e + e / 2;
144 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
145
146 Use *OldOps = op_begin();
147 BasicBlock **OldBlocks = block_begin();
Jay Foade03c05c2011-06-20 14:38:01 +0000148
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 ReservedSpace = NumOps;
Jay Foad61ea0e42011-06-23 09:09:15 +0000150 OperandList = allocHungoffUses(ReservedSpace);
151
152 std::copy(OldOps, OldOps + e, op_begin());
153 std::copy(OldBlocks, OldBlocks + e, block_begin());
154
Jay Foadbbb91f22011-01-16 15:30:52 +0000155 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156}
157
Nate Begemanb3923212005-08-04 23:24:19 +0000158/// hasConstantValue - If the specified PHI node always merges together the same
159/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000160Value *PHINode::hasConstantValue() const {
161 // Exploit the fact that phi nodes always have at least one entry.
162 Value *ConstantValue = getIncomingValue(0);
163 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000164 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
165 if (ConstantValue != this)
166 return 0; // Incoming values not all the same.
167 // The case where the first value is this PHI.
168 ConstantValue = getIncomingValue(i);
169 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000170 if (ConstantValue == this)
171 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000172 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000173}
174
Bill Wendlingfae14752011-08-12 20:24:12 +0000175//===----------------------------------------------------------------------===//
176// LandingPadInst Implementation
177//===----------------------------------------------------------------------===//
178
179LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
180 unsigned NumReservedValues, const Twine &NameStr,
181 Instruction *InsertBefore)
182 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) {
183 init(PersonalityFn, 1 + NumReservedValues, NameStr);
184}
185
186LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
187 unsigned NumReservedValues, const Twine &NameStr,
188 BasicBlock *InsertAtEnd)
189 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) {
190 init(PersonalityFn, 1 + NumReservedValues, NameStr);
191}
192
193LandingPadInst::LandingPadInst(const LandingPadInst &LP)
194 : Instruction(LP.getType(), Instruction::LandingPad,
195 allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()),
196 ReservedSpace(LP.getNumOperands()) {
197 Use *OL = OperandList, *InOL = LP.OperandList;
198 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
199 OL[I] = InOL[I];
200
201 setCleanup(LP.isCleanup());
202}
203
204LandingPadInst::~LandingPadInst() {
205 dropHungoffUses();
206}
207
208LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
209 unsigned NumReservedClauses,
210 const Twine &NameStr,
211 Instruction *InsertBefore) {
212 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
213 InsertBefore);
214}
215
216LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
217 unsigned NumReservedClauses,
218 const Twine &NameStr,
219 BasicBlock *InsertAtEnd) {
220 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
221 InsertAtEnd);
222}
223
224void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
225 const Twine &NameStr) {
226 ReservedSpace = NumReservedValues;
227 NumOperands = 1;
228 OperandList = allocHungoffUses(ReservedSpace);
229 OperandList[0] = PersFn;
230 setName(NameStr);
231 setCleanup(false);
232}
233
234/// growOperands - grow operands - This grows the operand list in response to a
235/// push_back style of operation. This grows the number of ops by 2 times.
236void LandingPadInst::growOperands(unsigned Size) {
237 unsigned e = getNumOperands();
238 if (ReservedSpace >= e + Size) return;
239 ReservedSpace = (e + Size / 2) * 2;
240
241 Use *NewOps = allocHungoffUses(ReservedSpace);
242 Use *OldOps = OperandList;
243 for (unsigned i = 0; i != e; ++i)
244 NewOps[i] = OldOps[i];
245
246 OperandList = NewOps;
247 Use::zap(OldOps, OldOps + e, true);
248}
249
250void LandingPadInst::addClause(Value *Val) {
251 unsigned OpNo = getNumOperands();
252 growOperands(1);
253 assert(OpNo < ReservedSpace && "Growing didn't work!");
254 ++NumOperands;
255 OperandList[OpNo] = Val;
256}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000257
258//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000259// CallInst Implementation
260//===----------------------------------------------------------------------===//
261
Gordon Henriksen14a55692007-12-10 02:14:30 +0000262CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000263}
264
Jay Foad5bd375a2011-07-15 08:37:34 +0000265void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
266 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000267 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268
Jay Foad5bd375a2011-07-15 08:37:34 +0000269#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000270 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000271 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
272
Jay Foad5bd375a2011-07-15 08:37:34 +0000273 assert((Args.size() == FTy->getNumParams() ||
274 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000275 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000276
277 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000278 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000279 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000280 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000281#endif
282
283 std::copy(Args.begin(), Args.end(), op_begin());
284 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285}
286
Jay Foad5bd375a2011-07-15 08:37:34 +0000287void CallInst::init(Value *Func, const Twine &NameStr) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000288 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000289 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000290
Jay Foad5bd375a2011-07-15 08:37:34 +0000291#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000292 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000293 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
294
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000295 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000296#endif
297
298 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299}
300
Daniel Dunbar4975db62009-07-25 04:41:11 +0000301CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 Instruction *InsertBefore)
303 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
304 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000305 Instruction::Call,
306 OperandTraits<CallInst>::op_end(this) - 1,
307 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000308 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309}
310
Daniel Dunbar4975db62009-07-25 04:41:11 +0000311CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 BasicBlock *InsertAtEnd)
313 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
314 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000315 Instruction::Call,
316 OperandTraits<CallInst>::op_end(this) - 1,
317 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000318 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319}
320
Misha Brukmanb1c93172005-04-21 23:48:37 +0000321CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000322 : Instruction(CI.getType(), Instruction::Call,
323 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000324 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000325 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000326 setTailCall(CI.isTailCall());
327 setCallingConv(CI.getCallingConv());
328
Jay Foad5bd375a2011-07-15 08:37:34 +0000329 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000330 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331}
332
Devang Patel4c758ea2008-09-25 21:00:45 +0000333void CallInst::addAttribute(unsigned i, Attributes attr) {
334 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000335 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000336 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000337}
338
Devang Patel4c758ea2008-09-25 21:00:45 +0000339void CallInst::removeAttribute(unsigned i, Attributes attr) {
340 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000341 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000342 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000343}
344
Devang Patelba3fa6c2008-09-23 23:03:40 +0000345bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000346 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000347 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000348 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000349 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000350 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000351}
352
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000353/// IsConstantOne - Return true only if val is constant int 1
354static bool IsConstantOne(Value *val) {
355 assert(val && "IsConstantOne does not work with NULL val");
356 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
357}
358
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000359static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000360 BasicBlock *InsertAtEnd, Type *IntPtrTy,
361 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000362 Value *ArraySize, Function *MallocF,
363 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000364 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000365 "createMalloc needs either InsertBefore or InsertAtEnd");
366
367 // malloc(type) becomes:
368 // bitcast (i8* malloc(typeSize)) to type*
369 // malloc(type, arraySize) becomes:
370 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000371 if (!ArraySize)
372 ArraySize = ConstantInt::get(IntPtrTy, 1);
373 else if (ArraySize->getType() != IntPtrTy) {
374 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000375 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
376 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000377 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000378 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
379 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000380 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000381
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000382 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000383 if (IsConstantOne(AllocSize)) {
384 AllocSize = ArraySize; // Operand * 1 = Operand
385 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
386 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
387 false /*ZExt*/);
388 // Malloc arg is constant product of type size and array size
389 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
390 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000391 // Multiply type size by the array size...
392 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000393 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
394 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000395 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000396 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
397 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000398 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000399 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000400
Victor Hernandez788eaab2009-09-18 19:20:02 +0000401 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000402 // Create the call to Malloc.
403 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
404 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000405 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000406 Value *MallocFunc = MallocF;
407 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000408 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000409 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000410 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000411 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000412 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000413 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000414 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000415 Result = MCall;
416 if (Result->getType() != AllocPtrType)
417 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000418 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000419 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000420 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000421 Result = MCall;
422 if (Result->getType() != AllocPtrType) {
423 InsertAtEnd->getInstList().push_back(MCall);
424 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000425 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000426 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000427 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000428 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000429 if (Function *F = dyn_cast<Function>(MallocFunc)) {
430 MCall->setCallingConv(F->getCallingConv());
431 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
432 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000433 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000434
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000435 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000436}
437
438/// CreateMalloc - Generate the IR for a call to malloc:
439/// 1. Compute the malloc call's argument as the specified type's size,
440/// possibly multiplied by the array size if the array size is not
441/// constant 1.
442/// 2. Call malloc with that argument.
443/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000444Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000445 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000446 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000447 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000448 const Twine &Name) {
449 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000450 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000451}
452
453/// CreateMalloc - Generate the IR for a call to malloc:
454/// 1. Compute the malloc call's argument as the specified type's size,
455/// possibly multiplied by the array size if the array size is not
456/// constant 1.
457/// 2. Call malloc with that argument.
458/// 3. Bitcast the result of the malloc call to the specified type.
459/// Note: This function does not add the bitcast to the basic block, that is the
460/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000461Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000462 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000463 Value *AllocSize, Value *ArraySize,
464 Function *MallocF, const Twine &Name) {
465 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000466 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000467}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000468
Victor Hernandeze2971492009-10-24 04:23:03 +0000469static Instruction* createFree(Value* Source, Instruction *InsertBefore,
470 BasicBlock *InsertAtEnd) {
471 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
472 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000473 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000474 "Can not free something of nonpointer type!");
475
476 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
477 Module* M = BB->getParent()->getParent();
478
Chris Lattner229907c2011-07-18 04:54:35 +0000479 Type *VoidTy = Type::getVoidTy(M->getContext());
480 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000481 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000482 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000483 CallInst* Result = NULL;
484 Value *PtrCast = Source;
485 if (InsertBefore) {
486 if (Source->getType() != IntPtrTy)
487 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
488 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
489 } else {
490 if (Source->getType() != IntPtrTy)
491 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
492 Result = CallInst::Create(FreeFunc, PtrCast, "");
493 }
494 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000495 if (Function *F = dyn_cast<Function>(FreeFunc))
496 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000497
498 return Result;
499}
500
501/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000502Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
503 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000504}
505
506/// CreateFree - Generate the IR for a call to the builtin free function.
507/// Note: This function does not add the call to the basic block, that is the
508/// responsibility of the caller.
509Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
510 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
511 assert(FreeCall && "CreateFree did not create a CallInst");
512 return FreeCall;
513}
514
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000515//===----------------------------------------------------------------------===//
516// InvokeInst Implementation
517//===----------------------------------------------------------------------===//
518
519void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000520 ArrayRef<Value *> Args, const Twine &NameStr) {
521 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000522 Op<-3>() = Fn;
523 Op<-2>() = IfNormal;
524 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000525
526#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000527 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000528 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000529
Jay Foad5bd375a2011-07-15 08:37:34 +0000530 assert(((Args.size() == FTy->getNumParams()) ||
531 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000532 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000533
Jay Foad5bd375a2011-07-15 08:37:34 +0000534 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000535 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000536 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000537 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000538#endif
539
540 std::copy(Args.begin(), Args.end(), op_begin());
541 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000542}
543
Misha Brukmanb1c93172005-04-21 23:48:37 +0000544InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000545 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000546 OperandTraits<InvokeInst>::op_end(this)
547 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000548 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000549 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000550 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000551 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000552 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000553}
554
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000555BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
556 return getSuccessor(idx);
557}
558unsigned InvokeInst::getNumSuccessorsV() const {
559 return getNumSuccessors();
560}
561void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
562 return setSuccessor(idx, B);
563}
564
Devang Patelba3fa6c2008-09-23 23:03:40 +0000565bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000566 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000567 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000568 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000569 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000570 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000571}
572
Devang Patel4c758ea2008-09-25 21:00:45 +0000573void InvokeInst::addAttribute(unsigned i, Attributes attr) {
574 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000575 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000576 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000577}
578
Devang Patel4c758ea2008-09-25 21:00:45 +0000579void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
580 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000581 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000582 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000583}
584
Bill Wendlingfae14752011-08-12 20:24:12 +0000585LandingPadInst *InvokeInst::getLandingPadInst() const {
586 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
587}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000588
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000589//===----------------------------------------------------------------------===//
590// ReturnInst Implementation
591//===----------------------------------------------------------------------===//
592
Chris Lattner2195fc42007-02-24 00:55:48 +0000593ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000594 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000595 OperandTraits<ReturnInst>::op_end(this) -
596 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000597 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000598 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000599 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000600 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000601}
602
Owen Anderson55f1c092009-08-13 21:58:54 +0000603ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
604 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000605 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
606 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000607 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000608 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000609}
Owen Anderson55f1c092009-08-13 21:58:54 +0000610ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
611 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000612 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
613 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000614 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000615 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000616}
Owen Anderson55f1c092009-08-13 21:58:54 +0000617ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
618 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000619 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000620}
621
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000622unsigned ReturnInst::getNumSuccessorsV() const {
623 return getNumSuccessors();
624}
625
Devang Patelae682fb2008-02-26 17:56:20 +0000626/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
627/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000629 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000630}
631
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000632BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000633 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000634}
635
Devang Patel59643e52008-02-23 00:35:18 +0000636ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000637}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000639//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000640// ResumeInst Implementation
641//===----------------------------------------------------------------------===//
642
643ResumeInst::ResumeInst(const ResumeInst &RI)
644 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
645 OperandTraits<ResumeInst>::op_begin(this), 1) {
646 Op<0>() = RI.Op<0>();
647}
648
649ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
650 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
651 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
652 Op<0>() = Exn;
653}
654
655ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
656 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
657 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
658 Op<0>() = Exn;
659}
660
661unsigned ResumeInst::getNumSuccessorsV() const {
662 return getNumSuccessors();
663}
664
665void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
666 llvm_unreachable("ResumeInst has no successors!");
667}
668
669BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
670 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000671}
672
673//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000674// UnreachableInst Implementation
675//===----------------------------------------------------------------------===//
676
Owen Anderson55f1c092009-08-13 21:58:54 +0000677UnreachableInst::UnreachableInst(LLVMContext &Context,
678 Instruction *InsertBefore)
679 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
680 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000681}
Owen Anderson55f1c092009-08-13 21:58:54 +0000682UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
683 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
684 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000685}
686
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687unsigned UnreachableInst::getNumSuccessorsV() const {
688 return getNumSuccessors();
689}
690
691void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000692 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000693}
694
695BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000696 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000697}
698
699//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000700// BranchInst Implementation
701//===----------------------------------------------------------------------===//
702
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000703void BranchInst::AssertOK() {
704 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000705 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000707}
708
Chris Lattner2195fc42007-02-24 00:55:48 +0000709BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000710 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000711 OperandTraits<BranchInst>::op_end(this) - 1,
712 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000713 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000714 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000715}
716BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
717 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000718 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000719 OperandTraits<BranchInst>::op_end(this) - 3,
720 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000721 Op<-1>() = IfTrue;
722 Op<-2>() = IfFalse;
723 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000724#ifndef NDEBUG
725 AssertOK();
726#endif
727}
728
729BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000730 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000731 OperandTraits<BranchInst>::op_end(this) - 1,
732 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000733 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000734 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000735}
736
737BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
738 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000739 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000740 OperandTraits<BranchInst>::op_end(this) - 3,
741 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000742 Op<-1>() = IfTrue;
743 Op<-2>() = IfFalse;
744 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000745#ifndef NDEBUG
746 AssertOK();
747#endif
748}
749
750
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000751BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000752 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000753 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
754 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000755 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000756 if (BI.getNumOperands() != 1) {
757 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000758 Op<-3>() = BI.Op<-3>();
759 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000761 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000762}
763
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000764void BranchInst::swapSuccessors() {
765 assert(isConditional() &&
766 "Cannot swap successors of an unconditional branch");
767 Op<-1>().swap(Op<-2>());
768
769 // Update profile metadata if present and it matches our structural
770 // expectations.
771 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
772 if (!ProfileData || ProfileData->getNumOperands() != 3)
773 return;
774
775 // The first operand is the name. Fetch them backwards and build a new one.
776 Value *Ops[] = {
777 ProfileData->getOperand(0),
778 ProfileData->getOperand(2),
779 ProfileData->getOperand(1)
780 };
781 setMetadata(LLVMContext::MD_prof,
782 MDNode::get(ProfileData->getContext(), Ops));
783}
784
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000785BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
786 return getSuccessor(idx);
787}
788unsigned BranchInst::getNumSuccessorsV() const {
789 return getNumSuccessors();
790}
791void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
792 setSuccessor(idx, B);
793}
794
795
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000796//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000797// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000798//===----------------------------------------------------------------------===//
799
Owen Andersonb6b25302009-07-14 23:09:55 +0000800static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000801 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000802 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000803 else {
804 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000805 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000806 assert(Amt->getType()->isIntegerTy() &&
807 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000808 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000809 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000810}
811
Chris Lattner229907c2011-07-18 04:54:35 +0000812AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000813 const Twine &Name, Instruction *InsertBefore)
814 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
815 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
816 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000817 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000818 setName(Name);
819}
820
Chris Lattner229907c2011-07-18 04:54:35 +0000821AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000822 const Twine &Name, BasicBlock *InsertAtEnd)
823 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
824 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
825 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000826 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000827 setName(Name);
828}
829
Chris Lattner229907c2011-07-18 04:54:35 +0000830AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000831 Instruction *InsertBefore)
832 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
833 getAISize(Ty->getContext(), 0), InsertBefore) {
834 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000835 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000836 setName(Name);
837}
838
Chris Lattner229907c2011-07-18 04:54:35 +0000839AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000840 BasicBlock *InsertAtEnd)
841 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
842 getAISize(Ty->getContext(), 0), InsertAtEnd) {
843 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000844 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000845 setName(Name);
846}
847
Chris Lattner229907c2011-07-18 04:54:35 +0000848AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000849 const Twine &Name, Instruction *InsertBefore)
850 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000851 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000852 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000853 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000854 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000855}
856
Chris Lattner229907c2011-07-18 04:54:35 +0000857AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000858 const Twine &Name, BasicBlock *InsertAtEnd)
859 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000860 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000861 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000862 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000863 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000864}
865
Gordon Henriksen14a55692007-12-10 02:14:30 +0000866// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000867AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000868}
869
Victor Hernandez8acf2952009-10-23 21:09:37 +0000870void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000871 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000872 assert(Align <= MaximumAlignment &&
873 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000874 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000875 assert(getAlignment() == Align && "Alignment representation error!");
876}
877
Victor Hernandez8acf2952009-10-23 21:09:37 +0000878bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000879 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000880 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000881 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000882}
883
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000884Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000885 return getType()->getElementType();
886}
887
Chris Lattner8b291e62008-11-26 02:54:17 +0000888/// isStaticAlloca - Return true if this alloca is in the entry block of the
889/// function and is a constant size. If so, the code generator will fold it
890/// into the prolog/epilog code, so it is basically free.
891bool AllocaInst::isStaticAlloca() const {
892 // Must be constant size.
893 if (!isa<ConstantInt>(getArraySize())) return false;
894
895 // Must be in the entry block.
896 const BasicBlock *Parent = getParent();
897 return Parent == &Parent->getParent()->front();
898}
899
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901// LoadInst Implementation
902//===----------------------------------------------------------------------===//
903
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000904void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000905 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000906 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000907 assert(!(isAtomic() && getAlignment() == 0) &&
908 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000909}
910
Daniel Dunbar4975db62009-07-25 04:41:11 +0000911LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000912 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000913 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000914 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000915 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000916 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000917 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000918 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000919}
920
Daniel Dunbar4975db62009-07-25 04:41:11 +0000921LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000923 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000924 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000925 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000926 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000927 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000928 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929}
930
Daniel Dunbar4975db62009-07-25 04:41:11 +0000931LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000932 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000933 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000934 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000935 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000936 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000937 setAtomic(NotAtomic);
938 AssertOK();
939 setName(Name);
940}
941
942LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
943 BasicBlock *InsertAE)
944 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
945 Load, Ptr, InsertAE) {
946 setVolatile(isVolatile);
947 setAlignment(0);
948 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +0000949 AssertOK();
950 setName(Name);
951}
952
Daniel Dunbar4975db62009-07-25 04:41:11 +0000953LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000954 unsigned Align, Instruction *InsertBef)
955 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
956 Load, Ptr, InsertBef) {
957 setVolatile(isVolatile);
958 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000959 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000960 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000961 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000962}
963
Daniel Dunbar4975db62009-07-25 04:41:11 +0000964LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000965 unsigned Align, BasicBlock *InsertAE)
966 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
967 Load, Ptr, InsertAE) {
968 setVolatile(isVolatile);
969 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000970 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +0000971 AssertOK();
972 setName(Name);
973}
974
Eli Friedman59b66882011-08-09 23:02:53 +0000975LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
976 unsigned Align, AtomicOrdering Order,
977 SynchronizationScope SynchScope,
978 Instruction *InsertBef)
979 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
980 Load, Ptr, InsertBef) {
981 setVolatile(isVolatile);
982 setAlignment(Align);
983 setAtomic(Order, SynchScope);
984 AssertOK();
985 setName(Name);
986}
987
988LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
989 unsigned Align, AtomicOrdering Order,
990 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000991 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000992 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000993 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000994 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +0000995 setAlignment(Align);
996 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +0000997 AssertOK();
998 setName(Name);
999}
1000
Daniel Dunbar27096822009-08-11 18:11:15 +00001001LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1002 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1003 Load, Ptr, InsertBef) {
1004 setVolatile(false);
1005 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001006 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001007 AssertOK();
1008 if (Name && Name[0]) setName(Name);
1009}
1010
1011LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1012 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1013 Load, Ptr, InsertAE) {
1014 setVolatile(false);
1015 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001016 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001017 AssertOK();
1018 if (Name && Name[0]) setName(Name);
1019}
1020
1021LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1022 Instruction *InsertBef)
1023: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1024 Load, Ptr, InsertBef) {
1025 setVolatile(isVolatile);
1026 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001027 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001028 AssertOK();
1029 if (Name && Name[0]) setName(Name);
1030}
1031
1032LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1033 BasicBlock *InsertAE)
1034 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1035 Load, Ptr, InsertAE) {
1036 setVolatile(isVolatile);
1037 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001038 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001039 AssertOK();
1040 if (Name && Name[0]) setName(Name);
1041}
1042
Christopher Lamb84485702007-04-22 19:24:39 +00001043void LoadInst::setAlignment(unsigned Align) {
1044 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001045 assert(Align <= MaximumAlignment &&
1046 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001047 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001048 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001049 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001050}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001051
1052//===----------------------------------------------------------------------===//
1053// StoreInst Implementation
1054//===----------------------------------------------------------------------===//
1055
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001056void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001057 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001058 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001059 "Ptr must have pointer type!");
1060 assert(getOperand(0)->getType() ==
1061 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001062 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001063 assert(!(isAtomic() && getAlignment() == 0) &&
1064 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001065}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001066
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001067
1068StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001069 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001070 OperandTraits<StoreInst>::op_begin(this),
1071 OperandTraits<StoreInst>::operands(this),
1072 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001073 Op<0>() = val;
1074 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001075 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001076 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001077 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001078 AssertOK();
1079}
1080
1081StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001082 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001083 OperandTraits<StoreInst>::op_begin(this),
1084 OperandTraits<StoreInst>::operands(this),
1085 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001086 Op<0>() = val;
1087 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001088 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001089 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001090 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001091 AssertOK();
1092}
1093
Misha Brukmanb1c93172005-04-21 23:48:37 +00001094StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001095 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001096 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001097 OperandTraits<StoreInst>::op_begin(this),
1098 OperandTraits<StoreInst>::operands(this),
1099 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001100 Op<0>() = val;
1101 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001102 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001103 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001104 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001105 AssertOK();
1106}
1107
1108StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1109 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001110 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001111 OperandTraits<StoreInst>::op_begin(this),
1112 OperandTraits<StoreInst>::operands(this),
1113 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001114 Op<0>() = val;
1115 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001116 setVolatile(isVolatile);
1117 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001118 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001119 AssertOK();
1120}
1121
Misha Brukmanb1c93172005-04-21 23:48:37 +00001122StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001123 unsigned Align, AtomicOrdering Order,
1124 SynchronizationScope SynchScope,
1125 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001126 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001127 OperandTraits<StoreInst>::op_begin(this),
1128 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001129 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001130 Op<0>() = val;
1131 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001132 setVolatile(isVolatile);
1133 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001134 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001135 AssertOK();
1136}
1137
1138StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001139 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001140 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001141 OperandTraits<StoreInst>::op_begin(this),
1142 OperandTraits<StoreInst>::operands(this),
1143 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001144 Op<0>() = val;
1145 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001146 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001147 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001148 setAtomic(NotAtomic);
1149 AssertOK();
1150}
1151
1152StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1153 unsigned Align, BasicBlock *InsertAtEnd)
1154 : Instruction(Type::getVoidTy(val->getContext()), Store,
1155 OperandTraits<StoreInst>::op_begin(this),
1156 OperandTraits<StoreInst>::operands(this),
1157 InsertAtEnd) {
1158 Op<0>() = val;
1159 Op<1>() = addr;
1160 setVolatile(isVolatile);
1161 setAlignment(Align);
1162 setAtomic(NotAtomic);
1163 AssertOK();
1164}
1165
1166StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1167 unsigned Align, AtomicOrdering Order,
1168 SynchronizationScope SynchScope,
1169 BasicBlock *InsertAtEnd)
1170 : Instruction(Type::getVoidTy(val->getContext()), Store,
1171 OperandTraits<StoreInst>::op_begin(this),
1172 OperandTraits<StoreInst>::operands(this),
1173 InsertAtEnd) {
1174 Op<0>() = val;
1175 Op<1>() = addr;
1176 setVolatile(isVolatile);
1177 setAlignment(Align);
1178 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001179 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001180}
1181
Christopher Lamb84485702007-04-22 19:24:39 +00001182void StoreInst::setAlignment(unsigned Align) {
1183 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001184 assert(Align <= MaximumAlignment &&
1185 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001186 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001187 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001188 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001189}
1190
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001191//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001192// AtomicCmpXchgInst Implementation
1193//===----------------------------------------------------------------------===//
1194
1195void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1196 AtomicOrdering Ordering,
1197 SynchronizationScope SynchScope) {
1198 Op<0>() = Ptr;
1199 Op<1>() = Cmp;
1200 Op<2>() = NewVal;
1201 setOrdering(Ordering);
1202 setSynchScope(SynchScope);
1203
1204 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1205 "All operands must be non-null!");
1206 assert(getOperand(0)->getType()->isPointerTy() &&
1207 "Ptr must have pointer type!");
1208 assert(getOperand(1)->getType() ==
1209 cast<PointerType>(getOperand(0)->getType())->getElementType()
1210 && "Ptr must be a pointer to Cmp type!");
1211 assert(getOperand(2)->getType() ==
1212 cast<PointerType>(getOperand(0)->getType())->getElementType()
1213 && "Ptr must be a pointer to NewVal type!");
1214 assert(Ordering != NotAtomic &&
1215 "AtomicCmpXchg instructions must be atomic!");
1216}
1217
1218AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1219 AtomicOrdering Ordering,
1220 SynchronizationScope SynchScope,
1221 Instruction *InsertBefore)
1222 : Instruction(Cmp->getType(), AtomicCmpXchg,
1223 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1224 OperandTraits<AtomicCmpXchgInst>::operands(this),
1225 InsertBefore) {
1226 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1227}
1228
1229AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1230 AtomicOrdering Ordering,
1231 SynchronizationScope SynchScope,
1232 BasicBlock *InsertAtEnd)
1233 : Instruction(Cmp->getType(), AtomicCmpXchg,
1234 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1235 OperandTraits<AtomicCmpXchgInst>::operands(this),
1236 InsertAtEnd) {
1237 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1238}
1239
1240//===----------------------------------------------------------------------===//
1241// AtomicRMWInst Implementation
1242//===----------------------------------------------------------------------===//
1243
1244void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1245 AtomicOrdering Ordering,
1246 SynchronizationScope SynchScope) {
1247 Op<0>() = Ptr;
1248 Op<1>() = Val;
1249 setOperation(Operation);
1250 setOrdering(Ordering);
1251 setSynchScope(SynchScope);
1252
1253 assert(getOperand(0) && getOperand(1) &&
1254 "All operands must be non-null!");
1255 assert(getOperand(0)->getType()->isPointerTy() &&
1256 "Ptr must have pointer type!");
1257 assert(getOperand(1)->getType() ==
1258 cast<PointerType>(getOperand(0)->getType())->getElementType()
1259 && "Ptr must be a pointer to Val type!");
1260 assert(Ordering != NotAtomic &&
1261 "AtomicRMW instructions must be atomic!");
1262}
1263
1264AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1265 AtomicOrdering Ordering,
1266 SynchronizationScope SynchScope,
1267 Instruction *InsertBefore)
1268 : Instruction(Val->getType(), AtomicRMW,
1269 OperandTraits<AtomicRMWInst>::op_begin(this),
1270 OperandTraits<AtomicRMWInst>::operands(this),
1271 InsertBefore) {
1272 Init(Operation, Ptr, Val, Ordering, SynchScope);
1273}
1274
1275AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1276 AtomicOrdering Ordering,
1277 SynchronizationScope SynchScope,
1278 BasicBlock *InsertAtEnd)
1279 : Instruction(Val->getType(), AtomicRMW,
1280 OperandTraits<AtomicRMWInst>::op_begin(this),
1281 OperandTraits<AtomicRMWInst>::operands(this),
1282 InsertAtEnd) {
1283 Init(Operation, Ptr, Val, Ordering, SynchScope);
1284}
1285
1286//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001287// FenceInst Implementation
1288//===----------------------------------------------------------------------===//
1289
1290FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1291 SynchronizationScope SynchScope,
1292 Instruction *InsertBefore)
1293 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1294 setOrdering(Ordering);
1295 setSynchScope(SynchScope);
1296}
1297
1298FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1299 SynchronizationScope SynchScope,
1300 BasicBlock *InsertAtEnd)
1301 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1302 setOrdering(Ordering);
1303 setSynchScope(SynchScope);
1304}
1305
1306//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001307// GetElementPtrInst Implementation
1308//===----------------------------------------------------------------------===//
1309
Jay Foadd1b78492011-07-25 09:48:08 +00001310void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001311 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001312 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1313 OperandList[0] = Ptr;
1314 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001315 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001316}
1317
Gabor Greiff6caff662008-05-10 08:32:32 +00001318GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001319 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001320 OperandTraits<GetElementPtrInst>::op_end(this)
1321 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001322 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001323 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001324 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001325}
1326
Chris Lattner6090a422009-03-09 04:46:40 +00001327/// getIndexedType - Returns the type of the element that would be accessed with
1328/// a gep instruction with the specified parameters.
1329///
1330/// The Idxs pointer should point to a continuous piece of memory containing the
1331/// indices, either as Value* or uint64_t.
1332///
1333/// A null type is returned if the indices are invalid for the specified
1334/// pointer type.
1335///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001336template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001337static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001338 if (Ptr->isVectorTy()) {
1339 assert(IdxList.size() == 1 &&
1340 "GEP with vector pointers must have a single index");
1341 PointerType *PTy = dyn_cast<PointerType>(
1342 cast<VectorType>(Ptr)->getElementType());
1343 assert(PTy && "Gep with invalid vector pointer found");
1344 return PTy->getElementType();
1345 }
1346
Chris Lattner229907c2011-07-18 04:54:35 +00001347 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001348 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001349 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001350
Chris Lattner6090a422009-03-09 04:46:40 +00001351 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001352 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001353 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001354
Chris Lattner6090a422009-03-09 04:46:40 +00001355 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001356 // it cannot be 'stepped over'.
1357 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001358 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001359
Dan Gohman1ecaf452008-05-31 00:58:22 +00001360 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001361 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001362 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001363 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001364 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001365 if (!CT->indexValid(Index)) return 0;
1366 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001367 }
Jay Foadd1b78492011-07-25 09:48:08 +00001368 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001369}
1370
Jay Foadd1b78492011-07-25 09:48:08 +00001371Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1372 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001373}
1374
Chris Lattner229907c2011-07-18 04:54:35 +00001375Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001376 ArrayRef<Constant *> IdxList) {
1377 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001378}
1379
Jay Foadd1b78492011-07-25 09:48:08 +00001380Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1381 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001382}
1383
Nadav Rotem3924cb02011-12-05 06:29:09 +00001384unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) {
1385 Type *Ty = Ptr->getType();
1386
1387 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1388 Ty = VTy->getElementType();
1389
1390 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
1391 return PTy->getAddressSpace();
1392
Craig Topperc514b542012-02-05 22:14:15 +00001393 llvm_unreachable("Invalid GEP pointer type");
Nadav Rotem3924cb02011-12-05 06:29:09 +00001394}
1395
Chris Lattner45f15572007-04-14 00:12:57 +00001396/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1397/// zeros. If so, the result pointer and the first operand have the same
1398/// value, just potentially different types.
1399bool GetElementPtrInst::hasAllZeroIndices() const {
1400 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1401 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1402 if (!CI->isZero()) return false;
1403 } else {
1404 return false;
1405 }
1406 }
1407 return true;
1408}
1409
Chris Lattner27058292007-04-27 20:35:56 +00001410/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1411/// constant integers. If so, the result pointer and the first operand have
1412/// a constant offset between them.
1413bool GetElementPtrInst::hasAllConstantIndices() const {
1414 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1415 if (!isa<ConstantInt>(getOperand(i)))
1416 return false;
1417 }
1418 return true;
1419}
1420
Dan Gohman1b849082009-09-07 23:54:19 +00001421void GetElementPtrInst::setIsInBounds(bool B) {
1422 cast<GEPOperator>(this)->setIsInBounds(B);
1423}
Chris Lattner45f15572007-04-14 00:12:57 +00001424
Nick Lewycky28a5f252009-09-27 21:33:04 +00001425bool GetElementPtrInst::isInBounds() const {
1426 return cast<GEPOperator>(this)->isInBounds();
1427}
1428
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001429//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001430// ExtractElementInst Implementation
1431//===----------------------------------------------------------------------===//
1432
1433ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001434 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001435 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001436 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001437 ExtractElement,
1438 OperandTraits<ExtractElementInst>::op_begin(this),
1439 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001440 assert(isValidOperands(Val, Index) &&
1441 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001442 Op<0>() = Val;
1443 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001444 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001445}
1446
1447ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001448 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001449 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001450 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001451 ExtractElement,
1452 OperandTraits<ExtractElementInst>::op_begin(this),
1453 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001454 assert(isValidOperands(Val, Index) &&
1455 "Invalid extractelement instruction operands!");
1456
Gabor Greif2d3024d2008-05-26 21:33:52 +00001457 Op<0>() = Val;
1458 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001459 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001460}
1461
Chris Lattner65511ff2006-10-05 06:24:58 +00001462
Chris Lattner54865b32006-04-08 04:05:48 +00001463bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001464 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001465 return false;
1466 return true;
1467}
1468
1469
Robert Bocchino23004482006-01-10 19:05:34 +00001470//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001471// InsertElementInst Implementation
1472//===----------------------------------------------------------------------===//
1473
Chris Lattner54865b32006-04-08 04:05:48 +00001474InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001475 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001476 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001477 : Instruction(Vec->getType(), InsertElement,
1478 OperandTraits<InsertElementInst>::op_begin(this),
1479 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001480 assert(isValidOperands(Vec, Elt, Index) &&
1481 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001482 Op<0>() = Vec;
1483 Op<1>() = Elt;
1484 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001485 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001486}
1487
Chris Lattner54865b32006-04-08 04:05:48 +00001488InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001489 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001490 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001491 : Instruction(Vec->getType(), InsertElement,
1492 OperandTraits<InsertElementInst>::op_begin(this),
1493 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001494 assert(isValidOperands(Vec, Elt, Index) &&
1495 "Invalid insertelement instruction operands!");
1496
Gabor Greif2d3024d2008-05-26 21:33:52 +00001497 Op<0>() = Vec;
1498 Op<1>() = Elt;
1499 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001500 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001501}
1502
Chris Lattner54865b32006-04-08 04:05:48 +00001503bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1504 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001505 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001506 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001507
Reid Spencerd84d35b2007-02-15 02:26:10 +00001508 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001509 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001510
Duncan Sands9dff9be2010-02-15 16:12:20 +00001511 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001512 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001513 return true;
1514}
1515
1516
Robert Bocchinoca27f032006-01-17 20:07:22 +00001517//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001518// ShuffleVectorInst Implementation
1519//===----------------------------------------------------------------------===//
1520
1521ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001522 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001523 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001524: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001525 cast<VectorType>(Mask->getType())->getNumElements()),
1526 ShuffleVector,
1527 OperandTraits<ShuffleVectorInst>::op_begin(this),
1528 OperandTraits<ShuffleVectorInst>::operands(this),
1529 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001530 assert(isValidOperands(V1, V2, Mask) &&
1531 "Invalid shuffle vector instruction operands!");
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
1538ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001539 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001540 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001541: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1542 cast<VectorType>(Mask->getType())->getNumElements()),
1543 ShuffleVector,
1544 OperandTraits<ShuffleVectorInst>::op_begin(this),
1545 OperandTraits<ShuffleVectorInst>::operands(this),
1546 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001547 assert(isValidOperands(V1, V2, Mask) &&
1548 "Invalid shuffle vector instruction operands!");
1549
Gabor Greif2d3024d2008-05-26 21:33:52 +00001550 Op<0>() = V1;
1551 Op<1>() = V2;
1552 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001553 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001554}
1555
Mon P Wang25f01062008-11-10 04:46:22 +00001556bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001557 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001558 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001559 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001560 return false;
1561
Chris Lattner1dcb6542012-01-25 23:49:49 +00001562 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001563 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001564 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001565 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001566
1567 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001568 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1569 return true;
1570
Nate Begeman60a31c32010-08-13 00:16:46 +00001571 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001572 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001573 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001574 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1575 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001576 return false;
1577 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1578 return false;
1579 }
1580 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001581 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001582 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001583
1584 if (const ConstantDataSequential *CDS =
1585 dyn_cast<ConstantDataSequential>(Mask)) {
1586 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1587 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1588 if (CDS->getElementAsInteger(i) >= V1Size*2)
1589 return false;
1590 return true;
1591 }
1592
1593 // The bitcode reader can create a place holder for a forward reference
1594 // used as the shuffle mask. When this occurs, the shuffle mask will
1595 // fall into this case and fail. To avoid this error, do this bit of
1596 // ugliness to allow such a mask pass.
1597 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1598 if (CE->getOpcode() == Instruction::UserOp1)
1599 return true;
1600
1601 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001602}
1603
Chris Lattnerf724e342008-03-02 05:28:33 +00001604/// getMaskValue - Return the index from the shuffle mask for the specified
1605/// output result. This is either -1 if the element is undef or a number less
1606/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001607int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1608 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1609 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001610 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001611 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001612 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001613 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001614 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001615}
1616
Chris Lattner1dcb6542012-01-25 23:49:49 +00001617/// getShuffleMask - Return the full mask for this instruction, where each
1618/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001619void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1620 SmallVectorImpl<int> &Result) {
1621 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001622
Chris Lattnercf129702012-01-26 02:51:13 +00001623 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001624 for (unsigned i = 0; i != NumElts; ++i)
1625 Result.push_back(CDS->getElementAsInteger(i));
1626 return;
1627 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001628 for (unsigned i = 0; i != NumElts; ++i) {
1629 Constant *C = Mask->getAggregateElement(i);
1630 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001631 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001632 }
1633}
1634
1635
Dan Gohman12fce772008-05-15 19:50:34 +00001636//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001637// InsertValueInst Class
1638//===----------------------------------------------------------------------===//
1639
Jay Foad57aa6362011-07-13 10:26:04 +00001640void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1641 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001642 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001643
1644 // There's no fundamental reason why we require at least one index
1645 // (other than weirdness with &*IdxBegin being invalid; see
1646 // getelementptr's init routine for example). But there's no
1647 // present need to support it.
1648 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1649
1650 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001651 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001652 Op<0>() = Agg;
1653 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001654
Jay Foad57aa6362011-07-13 10:26:04 +00001655 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001656 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001657}
1658
1659InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001660 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001661 OperandTraits<InsertValueInst>::op_begin(this), 2),
1662 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001663 Op<0>() = IVI.getOperand(0);
1664 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001665 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001666}
1667
1668//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001669// ExtractValueInst Class
1670//===----------------------------------------------------------------------===//
1671
Jay Foad57aa6362011-07-13 10:26:04 +00001672void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001673 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001674
Jay Foad57aa6362011-07-13 10:26:04 +00001675 // There's no fundamental reason why we require at least one index.
1676 // But there's no present need to support it.
1677 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001678
Jay Foad57aa6362011-07-13 10:26:04 +00001679 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001680 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001681}
1682
1683ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001684 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001685 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001686 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001687}
1688
Dan Gohman12fce772008-05-15 19:50:34 +00001689// getIndexedType - Returns the type of the element that would be extracted
1690// with an extractvalue instruction with the specified parameters.
1691//
1692// A null type is returned if the indices are invalid for the specified
1693// pointer type.
1694//
Chris Lattner229907c2011-07-18 04:54:35 +00001695Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001696 ArrayRef<unsigned> Idxs) {
1697 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001698 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001699 // We can't use CompositeType::indexValid(Index) here.
1700 // indexValid() always returns true for arrays because getelementptr allows
1701 // out-of-bounds indices. Since we don't allow those for extractvalue and
1702 // insertvalue we need to check array indexing manually.
1703 // Since the only other types we can index into are struct types it's just
1704 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001705 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001706 if (Index >= AT->getNumElements())
1707 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001708 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001709 if (Index >= ST->getNumElements())
1710 return 0;
1711 } else {
1712 // Not a valid type to index into.
1713 return 0;
1714 }
1715
1716 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001717 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001718 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001719}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001720
1721//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001722// BinaryOperator Class
1723//===----------------------------------------------------------------------===//
1724
Chris Lattner2195fc42007-02-24 00:55:48 +00001725BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001726 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001727 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001728 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001729 OperandTraits<BinaryOperator>::op_begin(this),
1730 OperandTraits<BinaryOperator>::operands(this),
1731 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001732 Op<0>() = S1;
1733 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001734 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001735 setName(Name);
1736}
1737
1738BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001739 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001740 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001741 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001742 OperandTraits<BinaryOperator>::op_begin(this),
1743 OperandTraits<BinaryOperator>::operands(this),
1744 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001745 Op<0>() = S1;
1746 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001747 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001748 setName(Name);
1749}
1750
1751
1752void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001753 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001754 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001755 assert(LHS->getType() == RHS->getType() &&
1756 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001757#ifndef NDEBUG
1758 switch (iType) {
1759 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001760 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001761 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001762 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001763 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001764 "Tried to create an integer operation on a non-integer type!");
1765 break;
1766 case FAdd: case FSub:
1767 case FMul:
1768 assert(getType() == LHS->getType() &&
1769 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001770 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001771 "Tried to create a floating-point operation on a "
1772 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001773 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001774 case UDiv:
1775 case SDiv:
1776 assert(getType() == LHS->getType() &&
1777 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001778 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001779 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001780 "Incorrect operand type (not integer) for S/UDIV");
1781 break;
1782 case FDiv:
1783 assert(getType() == LHS->getType() &&
1784 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001785 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001786 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001787 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001788 case URem:
1789 case SRem:
1790 assert(getType() == LHS->getType() &&
1791 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001792 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001793 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001794 "Incorrect operand type (not integer) for S/UREM");
1795 break;
1796 case FRem:
1797 assert(getType() == LHS->getType() &&
1798 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001799 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001800 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001801 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001802 case Shl:
1803 case LShr:
1804 case AShr:
1805 assert(getType() == LHS->getType() &&
1806 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001807 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001808 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001809 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001810 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001811 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001812 case And: case Or:
1813 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001814 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001815 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001816 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001817 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001818 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001819 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001820 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001821 default:
1822 break;
1823 }
1824#endif
1825}
1826
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001827BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001828 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001829 Instruction *InsertBefore) {
1830 assert(S1->getType() == S2->getType() &&
1831 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001832 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001833}
1834
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001835BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001836 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001837 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001838 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001839 InsertAtEnd->getInstList().push_back(Res);
1840 return Res;
1841}
1842
Dan Gohman5476cfd2009-08-12 16:23:25 +00001843BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001844 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001845 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001846 return new BinaryOperator(Instruction::Sub,
1847 zero, Op,
1848 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001849}
1850
Dan Gohman5476cfd2009-08-12 16:23:25 +00001851BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001852 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001853 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001854 return new BinaryOperator(Instruction::Sub,
1855 zero, Op,
1856 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001857}
1858
Dan Gohman4ab44202009-12-18 02:58:50 +00001859BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1860 Instruction *InsertBefore) {
1861 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1862 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1863}
1864
1865BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1866 BasicBlock *InsertAtEnd) {
1867 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1868 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1869}
1870
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001871BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1872 Instruction *InsertBefore) {
1873 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1874 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1875}
1876
1877BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1878 BasicBlock *InsertAtEnd) {
1879 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1880 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1881}
1882
Dan Gohman5476cfd2009-08-12 16:23:25 +00001883BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001884 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001885 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001886 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001887 Op->getType(), Name, InsertBefore);
1888}
1889
Dan Gohman5476cfd2009-08-12 16:23:25 +00001890BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001891 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001892 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001893 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001894 Op->getType(), Name, InsertAtEnd);
1895}
1896
Dan Gohman5476cfd2009-08-12 16:23:25 +00001897BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001898 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001899 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001900 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001901 Op->getType(), Name, InsertBefore);
1902}
1903
Dan Gohman5476cfd2009-08-12 16:23:25 +00001904BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001905 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001906 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001907 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001908 Op->getType(), Name, InsertAtEnd);
1909}
1910
1911
1912// isConstantAllOnes - Helper function for several functions below
1913static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001914 if (const Constant *C = dyn_cast<Constant>(V))
1915 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001916 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001917}
1918
Owen Andersonbb2501b2009-07-13 22:18:28 +00001919bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001920 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1921 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001922 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1923 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001924 return false;
1925}
1926
Owen Andersonbb2501b2009-07-13 22:18:28 +00001927bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001928 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1929 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001930 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001931 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001932 return false;
1933}
1934
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001935bool BinaryOperator::isNot(const Value *V) {
1936 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1937 return (Bop->getOpcode() == Instruction::Xor &&
1938 (isConstantAllOnes(Bop->getOperand(1)) ||
1939 isConstantAllOnes(Bop->getOperand(0))));
1940 return false;
1941}
1942
Chris Lattner2c7d1772005-04-24 07:28:37 +00001943Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001944 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001945}
1946
Chris Lattner2c7d1772005-04-24 07:28:37 +00001947const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1948 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001949}
1950
Dan Gohmana5b96452009-06-04 22:49:04 +00001951Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001952 return cast<BinaryOperator>(BinOp)->getOperand(1);
1953}
1954
1955const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1956 return getFNegArgument(const_cast<Value*>(BinOp));
1957}
1958
Chris Lattner2c7d1772005-04-24 07:28:37 +00001959Value *BinaryOperator::getNotArgument(Value *BinOp) {
1960 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1961 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1962 Value *Op0 = BO->getOperand(0);
1963 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001964 if (isConstantAllOnes(Op0)) return Op1;
1965
1966 assert(isConstantAllOnes(Op1));
1967 return Op0;
1968}
1969
Chris Lattner2c7d1772005-04-24 07:28:37 +00001970const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1971 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001972}
1973
1974
1975// swapOperands - Exchange the two operands to this instruction. This
1976// instruction is safe to use on any binary instruction and does not
1977// modify the semantics of the instruction. If the instruction is
1978// order dependent (SetLT f.e.) the opcode is changed.
1979//
1980bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001981 if (!isCommutative())
1982 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001983 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001984 return false;
1985}
1986
Dan Gohman1b849082009-09-07 23:54:19 +00001987void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1988 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1989}
1990
1991void BinaryOperator::setHasNoSignedWrap(bool b) {
1992 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1993}
1994
1995void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001996 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001997}
1998
Nick Lewycky28a5f252009-09-27 21:33:04 +00001999bool BinaryOperator::hasNoUnsignedWrap() const {
2000 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2001}
2002
2003bool BinaryOperator::hasNoSignedWrap() const {
2004 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2005}
2006
2007bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002008 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002009}
2010
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002011//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002012// FPMathOperator Class
2013//===----------------------------------------------------------------------===//
2014
2015/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2016/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002017/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002018float FPMathOperator::getFPAccuracy() const {
2019 const MDNode *MD =
2020 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2021 if (!MD)
2022 return 0.0;
Duncan Sands9af62982012-04-16 19:39:33 +00002023 ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2024 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002025}
2026
2027
2028//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002029// CastInst Class
2030//===----------------------------------------------------------------------===//
2031
David Blaikie3a15e142011-12-01 08:00:17 +00002032void CastInst::anchor() {}
2033
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002034// Just determine if this cast only deals with integral->integral conversion.
2035bool CastInst::isIntegerCast() const {
2036 switch (getOpcode()) {
2037 default: return false;
2038 case Instruction::ZExt:
2039 case Instruction::SExt:
2040 case Instruction::Trunc:
2041 return true;
2042 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002043 return getOperand(0)->getType()->isIntegerTy() &&
2044 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002046}
2047
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002048bool CastInst::isLosslessCast() const {
2049 // Only BitCast can be lossless, exit fast if we're not BitCast
2050 if (getOpcode() != Instruction::BitCast)
2051 return false;
2052
2053 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002054 Type* SrcTy = getOperand(0)->getType();
2055 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056 if (SrcTy == DstTy)
2057 return true;
2058
Reid Spencer8d9336d2006-12-31 05:26:44 +00002059 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002060 if (SrcTy->isPointerTy())
2061 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002062 return false; // Other types have no identity values
2063}
2064
2065/// This function determines if the CastInst does not require any bits to be
2066/// changed in order to effect the cast. Essentially, it identifies cases where
2067/// no code gen is necessary for the cast, hence the name no-op cast. For
2068/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002069/// # bitcast i32* %x to i8*
2070/// # bitcast <2 x i32> %x to <4 x i16>
2071/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002072/// @brief Determine if the described cast is a no-op.
2073bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002074 Type *SrcTy,
2075 Type *DestTy,
2076 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002077 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002078 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002079 case Instruction::Trunc:
2080 case Instruction::ZExt:
2081 case Instruction::SExt:
2082 case Instruction::FPTrunc:
2083 case Instruction::FPExt:
2084 case Instruction::UIToFP:
2085 case Instruction::SIToFP:
2086 case Instruction::FPToUI:
2087 case Instruction::FPToSI:
2088 return false; // These always modify bits
2089 case Instruction::BitCast:
2090 return true; // BitCast never modifies bits.
2091 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002092 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002093 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002095 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002096 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002097 }
2098}
2099
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002100/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002101bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002102 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2103}
2104
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002105/// This function determines if a pair of casts can be eliminated and what
2106/// opcode should be used in the elimination. This assumes that there are two
2107/// instructions like this:
2108/// * %F = firstOpcode SrcTy %x to MidTy
2109/// * %S = secondOpcode MidTy %F to DstTy
2110/// The function returns a resultOpcode so these two casts can be replaced with:
2111/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2112/// If no such cast is permited, the function returns 0.
2113unsigned CastInst::isEliminableCastPair(
2114 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002115 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002116 // Define the 144 possibilities for these two cast instructions. The values
2117 // in this matrix determine what to do in a given situation and select the
2118 // case in the switch below. The rows correspond to firstOp, the columns
2119 // correspond to secondOp. In looking at the table below, keep in mind
2120 // the following cast properties:
2121 //
2122 // Size Compare Source Destination
2123 // Operator Src ? Size Type Sign Type Sign
2124 // -------- ------------ ------------------- ---------------------
2125 // TRUNC > Integer Any Integral Any
2126 // ZEXT < Integral Unsigned Integer Any
2127 // SEXT < Integral Signed Integer Any
2128 // FPTOUI n/a FloatPt n/a Integral Unsigned
2129 // FPTOSI n/a FloatPt n/a Integral Signed
2130 // UITOFP n/a Integral Unsigned FloatPt n/a
2131 // SITOFP n/a Integral Signed FloatPt n/a
2132 // FPTRUNC > FloatPt n/a FloatPt n/a
2133 // FPEXT < FloatPt n/a FloatPt n/a
2134 // PTRTOINT n/a Pointer n/a Integral Unsigned
2135 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002136 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002137 //
2138 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002139 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2140 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002141 // of the produced value (we no longer know the top-part is all zeros).
2142 // Further this conversion is often much more expensive for typical hardware,
2143 // and causes issues when building libgcc. We disallow fptosi+sext for the
2144 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002145 const unsigned numCastOps =
2146 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2147 static const uint8_t CastResults[numCastOps][numCastOps] = {
2148 // T F F U S F F P I B -+
2149 // R Z S P P I I T P 2 N T |
2150 // U E E 2 2 2 2 R E I T C +- secondOp
2151 // N X X U S F F N X N 2 V |
2152 // C T T I I P P C T T P T -+
2153 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2154 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2155 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002156 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2157 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002158 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2159 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2160 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2161 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2162 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2163 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2164 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2165 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002166
2167 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002168 // merging. However, bitcast of A->B->A are allowed.
2169 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2170 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2171 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2172
2173 // Check if any of the bitcasts convert scalars<->vectors.
2174 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2175 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2176 // Unless we are bitcasing to the original type, disallow optimizations.
2177 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178
2179 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2180 [secondOp-Instruction::CastOpsBegin];
2181 switch (ElimCase) {
2182 case 0:
2183 // categorically disallowed
2184 return 0;
2185 case 1:
2186 // allowed, use first cast's opcode
2187 return firstOp;
2188 case 2:
2189 // allowed, use second cast's opcode
2190 return secondOp;
2191 case 3:
2192 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002193 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002194 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002195 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196 return firstOp;
2197 return 0;
2198 case 4:
2199 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002200 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002201 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002202 return firstOp;
2203 return 0;
2204 case 5:
2205 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002206 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002207 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002208 return secondOp;
2209 return 0;
2210 case 6:
2211 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002212 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002213 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002214 return secondOp;
2215 return 0;
2216 case 7: {
2217 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002218 if (!IntPtrTy)
2219 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002220 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2221 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222 if (MidSize >= PtrSize)
2223 return Instruction::BitCast;
2224 return 0;
2225 }
2226 case 8: {
2227 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2228 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2229 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002230 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2231 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232 if (SrcSize == DstSize)
2233 return Instruction::BitCast;
2234 else if (SrcSize < DstSize)
2235 return firstOp;
2236 return secondOp;
2237 }
2238 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2239 return Instruction::ZExt;
2240 case 10:
2241 // fpext followed by ftrunc is allowed if the bit size returned to is
2242 // the same as the original, in which case its just a bitcast
2243 if (SrcTy == DstTy)
2244 return Instruction::BitCast;
2245 return 0; // If the types are not the same we can't eliminate it.
2246 case 11:
2247 // bitcast followed by ptrtoint is allowed as long as the bitcast
2248 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002249 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250 return secondOp;
2251 return 0;
2252 case 12:
2253 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002254 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002255 return firstOp;
2256 return 0;
2257 case 13: {
2258 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002259 if (!IntPtrTy)
2260 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002261 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2262 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2263 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264 if (SrcSize <= PtrSize && SrcSize == DstSize)
2265 return Instruction::BitCast;
2266 return 0;
2267 }
2268 case 99:
2269 // cast combination can't happen (error in input). This is for all cases
2270 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002271 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002272 default:
Craig Topperc514b542012-02-05 22:14:15 +00002273 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002274 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275}
2276
Chris Lattner229907c2011-07-18 04:54:35 +00002277CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002278 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002279 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002280 // Construct and return the appropriate CastInst subclass
2281 switch (op) {
2282 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2283 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2284 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2285 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2286 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2287 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2288 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2289 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2290 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2291 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2292 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2293 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topperc514b542012-02-05 22:14:15 +00002294 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296}
2297
Chris Lattner229907c2011-07-18 04:54:35 +00002298CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002299 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002300 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301 // Construct and return the appropriate CastInst subclass
2302 switch (op) {
2303 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2304 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2305 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2306 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2307 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2308 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2309 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2310 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2311 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2312 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2313 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2314 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topperc514b542012-02-05 22:14:15 +00002315 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317}
2318
Chris Lattner229907c2011-07-18 04:54:35 +00002319CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002320 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002321 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002322 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002323 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2324 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002325}
2326
Chris Lattner229907c2011-07-18 04:54:35 +00002327CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002328 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002329 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002330 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002331 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2332 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002333}
2334
Chris Lattner229907c2011-07-18 04:54:35 +00002335CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002336 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002337 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002338 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002339 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2340 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002341}
2342
Chris Lattner229907c2011-07-18 04:54:35 +00002343CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002344 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002345 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002346 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002347 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2348 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002349}
2350
Chris Lattner229907c2011-07-18 04:54:35 +00002351CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002352 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002353 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002354 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002355 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2356 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002357}
2358
Chris Lattner229907c2011-07-18 04:54:35 +00002359CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002360 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002361 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002362 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002363 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2364 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002365}
2366
Chris Lattner229907c2011-07-18 04:54:35 +00002367CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002368 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002369 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002370 assert(S->getType()->isPointerTy() && "Invalid cast");
2371 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002372 "Invalid cast");
2373
Duncan Sands9dff9be2010-02-15 16:12:20 +00002374 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002375 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2376 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002377}
2378
2379/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002380CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002381 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002382 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002383 assert(S->getType()->isPointerTy() && "Invalid cast");
2384 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002385 "Invalid cast");
2386
Duncan Sands9dff9be2010-02-15 16:12:20 +00002387 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002388 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2389 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002390}
2391
Chris Lattner229907c2011-07-18 04:54:35 +00002392CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002393 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002394 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002395 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002396 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002397 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2398 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002399 Instruction::CastOps opcode =
2400 (SrcBits == DstBits ? Instruction::BitCast :
2401 (SrcBits > DstBits ? Instruction::Trunc :
2402 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002403 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002404}
2405
Chris Lattner229907c2011-07-18 04:54:35 +00002406CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002407 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002408 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002409 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002410 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002411 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2412 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002413 Instruction::CastOps opcode =
2414 (SrcBits == DstBits ? Instruction::BitCast :
2415 (SrcBits > DstBits ? Instruction::Trunc :
2416 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002417 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002418}
2419
Chris Lattner229907c2011-07-18 04:54:35 +00002420CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002421 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002422 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002423 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002424 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002425 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2426 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002427 Instruction::CastOps opcode =
2428 (SrcBits == DstBits ? Instruction::BitCast :
2429 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002430 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002431}
2432
Chris Lattner229907c2011-07-18 04:54:35 +00002433CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002434 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002435 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002436 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002437 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002438 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2439 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002440 Instruction::CastOps opcode =
2441 (SrcBits == DstBits ? Instruction::BitCast :
2442 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002443 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002444}
2445
Duncan Sands55e50902008-01-06 10:12:28 +00002446// Check whether it is valid to call getCastOpcode for these types.
2447// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002448bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002449 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2450 return false;
2451
2452 if (SrcTy == DestTy)
2453 return true;
2454
Chris Lattner229907c2011-07-18 04:54:35 +00002455 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2456 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002457 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2458 // An element by element cast. Valid if casting the elements is valid.
2459 SrcTy = SrcVecTy->getElementType();
2460 DestTy = DestVecTy->getElementType();
2461 }
2462
Duncan Sands55e50902008-01-06 10:12:28 +00002463 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002464 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2465 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002466
2467 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002468 if (DestTy->isIntegerTy()) { // Casting to integral
2469 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002470 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002471 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002472 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002473 } else if (SrcTy->isVectorTy()) { // Casting from vector
2474 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002475 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002476 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002477 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002478 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2479 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002480 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002481 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002482 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002483 } else if (SrcTy->isVectorTy()) { // Casting from vector
2484 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002485 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002486 return false;
2487 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002488 } else if (DestTy->isVectorTy()) { // Casting to vector
2489 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002490 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002491 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002492 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002493 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002494 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002495 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002496 return false;
2497 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002498 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002499 if (SrcTy->isVectorTy()) {
2500 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002501 } else {
2502 return false;
2503 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002504 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002505 return false;
2506 }
2507}
2508
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509// Provide a way to get a "cast" where the cast opcode is inferred from the
2510// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002511// logic in the castIsValid function below. This axiom should hold:
2512// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2513// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002515// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002517CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002518 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2519 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520
Duncan Sands55e50902008-01-06 10:12:28 +00002521 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2522 "Only first class types are castable!");
2523
Duncan Sandsa8514532011-05-18 07:13:41 +00002524 if (SrcTy == DestTy)
2525 return BitCast;
2526
Chris Lattner229907c2011-07-18 04:54:35 +00002527 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2528 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002529 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2530 // An element by element cast. Find the appropriate opcode based on the
2531 // element types.
2532 SrcTy = SrcVecTy->getElementType();
2533 DestTy = DestVecTy->getElementType();
2534 }
2535
2536 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002537 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2538 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002539
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002541 if (DestTy->isIntegerTy()) { // Casting to integral
2542 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543 if (DestBits < SrcBits)
2544 return Trunc; // int -> smaller int
2545 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002546 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547 return SExt; // signed -> SEXT
2548 else
2549 return ZExt; // unsigned -> ZEXT
2550 } else {
2551 return BitCast; // Same size, No-op cast
2552 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002553 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002554 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555 return FPToSI; // FP -> sint
2556 else
2557 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002558 } else if (SrcTy->isVectorTy()) {
2559 assert(DestBits == SrcBits &&
2560 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561 return BitCast; // Same size, no-op cast
2562 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002563 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564 "Casting from a value that is not first-class type");
2565 return PtrToInt; // ptr -> int
2566 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002567 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2568 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002569 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570 return SIToFP; // sint -> FP
2571 else
2572 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002573 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574 if (DestBits < SrcBits) {
2575 return FPTrunc; // FP -> smaller FP
2576 } else if (DestBits > SrcBits) {
2577 return FPExt; // FP -> larger FP
2578 } else {
2579 return BitCast; // same size, no-op cast
2580 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002581 } else if (SrcTy->isVectorTy()) {
2582 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002583 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002584 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002586 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002587 } else if (DestTy->isVectorTy()) {
2588 assert(DestBits == SrcBits &&
2589 "Illegal cast to vector (wrong type or size)");
2590 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002591 } else if (DestTy->isPointerTy()) {
2592 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002594 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002596 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002597 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002598 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002599 if (SrcTy->isVectorTy()) {
2600 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002601 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002602 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002603 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002605 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606}
2607
2608//===----------------------------------------------------------------------===//
2609// CastInst SubClass Constructors
2610//===----------------------------------------------------------------------===//
2611
2612/// Check that the construction parameters for a CastInst are correct. This
2613/// could be broken out into the separate constructors but it is useful to have
2614/// it in one place and to eliminate the redundant code for getting the sizes
2615/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002616bool
Chris Lattner229907c2011-07-18 04:54:35 +00002617CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618
2619 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002620 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002621 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2622 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623 return false;
2624
2625 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002626 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2627 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628
Duncan Sands7f646562011-05-18 09:21:57 +00002629 // If these are vector types, get the lengths of the vectors (using zero for
2630 // scalar types means that checking that vector lengths match also checks that
2631 // scalars are not being converted to vectors or vectors to scalars).
2632 unsigned SrcLength = SrcTy->isVectorTy() ?
2633 cast<VectorType>(SrcTy)->getNumElements() : 0;
2634 unsigned DstLength = DstTy->isVectorTy() ?
2635 cast<VectorType>(DstTy)->getNumElements() : 0;
2636
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637 // Switch on the opcode provided
2638 switch (op) {
2639 default: return false; // This is an input error
2640 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002641 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2642 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002644 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2645 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002647 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2648 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002649 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002650 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2651 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002653 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2654 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002655 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002656 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002657 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2658 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002661 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2662 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002663 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002664 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002665 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002666 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2667 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2668 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002669 return SrcTy->getScalarType()->isPointerTy() &&
2670 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002672 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002673 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002674 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2675 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2676 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002677 return SrcTy->getScalarType()->isIntegerTy() &&
2678 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002679 case Instruction::BitCast:
2680 // BitCast implies a no-op cast of type only. No bits change.
2681 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002682 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002683 return false;
2684
Duncan Sands55e50902008-01-06 10:12:28 +00002685 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002686 // these cases, the cast is okay if the source and destination bit widths
2687 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002688 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002689 }
2690}
2691
2692TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002693 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002694) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002695 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002696}
2697
2698TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002699 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002700) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002701 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002702}
2703
2704ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002705 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002706) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002707 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002708}
2709
2710ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002711 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002712) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002713 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002714}
2715SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002716 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002717) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002718 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002719}
2720
Jeff Cohencc08c832006-12-02 02:22:01 +00002721SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002722 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002723) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002724 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002725}
2726
2727FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002728 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002729) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002730 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002731}
2732
2733FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002734 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002735) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002736 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002737}
2738
2739FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002740 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002741) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002742 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002743}
2744
2745FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002746 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002747) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002748 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002749}
2750
2751UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002752 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002753) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002754 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002755}
2756
2757UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002758 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002759) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002760 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002761}
2762
2763SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002764 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002765) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002766 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002767}
2768
2769SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002770 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002771) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002772 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002773}
2774
2775FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002776 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002777) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002778 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002779}
2780
2781FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002782 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002783) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002784 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002785}
2786
2787FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002788 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002789) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002790 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002791}
2792
2793FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002794 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002795) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002796 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002797}
2798
2799PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002800 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002801) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002802 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002803}
2804
2805PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002806 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002807) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002808 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002809}
2810
2811IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002812 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002813) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002814 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002815}
2816
2817IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002818 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002819) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002820 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002821}
2822
2823BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002824 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002825) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002826 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002827}
2828
2829BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002830 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002831) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002832 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002833}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002834
2835//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002836// CmpInst Classes
2837//===----------------------------------------------------------------------===//
2838
Craig Topper3186c012012-09-23 02:12:10 +00002839void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002840
Chris Lattner229907c2011-07-18 04:54:35 +00002841CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002842 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002843 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002844 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002845 OperandTraits<CmpInst>::op_begin(this),
2846 OperandTraits<CmpInst>::operands(this),
2847 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002848 Op<0>() = LHS;
2849 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002850 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002851 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002852}
Gabor Greiff6caff662008-05-10 08:32:32 +00002853
Chris Lattner229907c2011-07-18 04:54:35 +00002854CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002855 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002856 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002857 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002858 OperandTraits<CmpInst>::op_begin(this),
2859 OperandTraits<CmpInst>::operands(this),
2860 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002861 Op<0>() = LHS;
2862 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002863 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002864 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002865}
2866
2867CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002868CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002869 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002870 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002871 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002872 if (InsertBefore)
2873 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2874 S1, S2, Name);
2875 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002876 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002877 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002878 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002879
2880 if (InsertBefore)
2881 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2882 S1, S2, Name);
2883 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002884 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002885 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002886}
2887
2888CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002889CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002890 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002891 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002892 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2893 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002894 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002895 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2896 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002897}
2898
2899void CmpInst::swapOperands() {
2900 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2901 IC->swapOperands();
2902 else
2903 cast<FCmpInst>(this)->swapOperands();
2904}
2905
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002906bool CmpInst::isCommutative() const {
2907 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002908 return IC->isCommutative();
2909 return cast<FCmpInst>(this)->isCommutative();
2910}
2911
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002912bool CmpInst::isEquality() const {
2913 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002914 return IC->isEquality();
2915 return cast<FCmpInst>(this)->isEquality();
2916}
2917
2918
Dan Gohman4e724382008-05-31 02:47:54 +00002919CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002920 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002921 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002922 case ICMP_EQ: return ICMP_NE;
2923 case ICMP_NE: return ICMP_EQ;
2924 case ICMP_UGT: return ICMP_ULE;
2925 case ICMP_ULT: return ICMP_UGE;
2926 case ICMP_UGE: return ICMP_ULT;
2927 case ICMP_ULE: return ICMP_UGT;
2928 case ICMP_SGT: return ICMP_SLE;
2929 case ICMP_SLT: return ICMP_SGE;
2930 case ICMP_SGE: return ICMP_SLT;
2931 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002932
Dan Gohman4e724382008-05-31 02:47:54 +00002933 case FCMP_OEQ: return FCMP_UNE;
2934 case FCMP_ONE: return FCMP_UEQ;
2935 case FCMP_OGT: return FCMP_ULE;
2936 case FCMP_OLT: return FCMP_UGE;
2937 case FCMP_OGE: return FCMP_ULT;
2938 case FCMP_OLE: return FCMP_UGT;
2939 case FCMP_UEQ: return FCMP_ONE;
2940 case FCMP_UNE: return FCMP_OEQ;
2941 case FCMP_UGT: return FCMP_OLE;
2942 case FCMP_ULT: return FCMP_OGE;
2943 case FCMP_UGE: return FCMP_OLT;
2944 case FCMP_ULE: return FCMP_OGT;
2945 case FCMP_ORD: return FCMP_UNO;
2946 case FCMP_UNO: return FCMP_ORD;
2947 case FCMP_TRUE: return FCMP_FALSE;
2948 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002949 }
2950}
2951
Reid Spencer266e42b2006-12-23 06:05:41 +00002952ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2953 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002954 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002955 case ICMP_EQ: case ICMP_NE:
2956 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2957 return pred;
2958 case ICMP_UGT: return ICMP_SGT;
2959 case ICMP_ULT: return ICMP_SLT;
2960 case ICMP_UGE: return ICMP_SGE;
2961 case ICMP_ULE: return ICMP_SLE;
2962 }
2963}
2964
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002965ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2966 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002967 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002968 case ICMP_EQ: case ICMP_NE:
2969 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2970 return pred;
2971 case ICMP_SGT: return ICMP_UGT;
2972 case ICMP_SLT: return ICMP_ULT;
2973 case ICMP_SGE: return ICMP_UGE;
2974 case ICMP_SLE: return ICMP_ULE;
2975 }
2976}
2977
Reid Spencer0286bc12007-02-28 22:00:54 +00002978/// Initialize a set of values that all satisfy the condition with C.
2979///
2980ConstantRange
2981ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2982 APInt Lower(C);
2983 APInt Upper(C);
2984 uint32_t BitWidth = C.getBitWidth();
2985 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002986 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002987 case ICmpInst::ICMP_EQ: Upper++; break;
2988 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002989 case ICmpInst::ICMP_ULT:
2990 Lower = APInt::getMinValue(BitWidth);
2991 // Check for an empty-set condition.
2992 if (Lower == Upper)
2993 return ConstantRange(BitWidth, /*isFullSet=*/false);
2994 break;
2995 case ICmpInst::ICMP_SLT:
2996 Lower = APInt::getSignedMinValue(BitWidth);
2997 // Check for an empty-set condition.
2998 if (Lower == Upper)
2999 return ConstantRange(BitWidth, /*isFullSet=*/false);
3000 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003001 case ICmpInst::ICMP_UGT:
3002 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003003 // Check for an empty-set condition.
3004 if (Lower == Upper)
3005 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003006 break;
3007 case ICmpInst::ICMP_SGT:
3008 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003009 // Check for an empty-set condition.
3010 if (Lower == Upper)
3011 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003012 break;
3013 case ICmpInst::ICMP_ULE:
3014 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003015 // Check for a full-set condition.
3016 if (Lower == Upper)
3017 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003018 break;
3019 case ICmpInst::ICMP_SLE:
3020 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003021 // Check for a full-set condition.
3022 if (Lower == Upper)
3023 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003024 break;
3025 case ICmpInst::ICMP_UGE:
3026 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003027 // Check for a full-set condition.
3028 if (Lower == Upper)
3029 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003030 break;
3031 case ICmpInst::ICMP_SGE:
3032 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003033 // Check for a full-set condition.
3034 if (Lower == Upper)
3035 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003036 break;
3037 }
3038 return ConstantRange(Lower, Upper);
3039}
3040
Dan Gohman4e724382008-05-31 02:47:54 +00003041CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003042 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003043 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003044 case ICMP_EQ: case ICMP_NE:
3045 return pred;
3046 case ICMP_SGT: return ICMP_SLT;
3047 case ICMP_SLT: return ICMP_SGT;
3048 case ICMP_SGE: return ICMP_SLE;
3049 case ICMP_SLE: return ICMP_SGE;
3050 case ICMP_UGT: return ICMP_ULT;
3051 case ICMP_ULT: return ICMP_UGT;
3052 case ICMP_UGE: return ICMP_ULE;
3053 case ICMP_ULE: return ICMP_UGE;
3054
Reid Spencerd9436b62006-11-20 01:22:35 +00003055 case FCMP_FALSE: case FCMP_TRUE:
3056 case FCMP_OEQ: case FCMP_ONE:
3057 case FCMP_UEQ: case FCMP_UNE:
3058 case FCMP_ORD: case FCMP_UNO:
3059 return pred;
3060 case FCMP_OGT: return FCMP_OLT;
3061 case FCMP_OLT: return FCMP_OGT;
3062 case FCMP_OGE: return FCMP_OLE;
3063 case FCMP_OLE: return FCMP_OGE;
3064 case FCMP_UGT: return FCMP_ULT;
3065 case FCMP_ULT: return FCMP_UGT;
3066 case FCMP_UGE: return FCMP_ULE;
3067 case FCMP_ULE: return FCMP_UGE;
3068 }
3069}
3070
Reid Spencer266e42b2006-12-23 06:05:41 +00003071bool CmpInst::isUnsigned(unsigned short predicate) {
3072 switch (predicate) {
3073 default: return false;
3074 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3075 case ICmpInst::ICMP_UGE: return true;
3076 }
3077}
3078
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003079bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003080 switch (predicate) {
3081 default: return false;
3082 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3083 case ICmpInst::ICMP_SGE: return true;
3084 }
3085}
3086
3087bool CmpInst::isOrdered(unsigned short predicate) {
3088 switch (predicate) {
3089 default: return false;
3090 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3091 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3092 case FCmpInst::FCMP_ORD: return true;
3093 }
3094}
3095
3096bool CmpInst::isUnordered(unsigned short predicate) {
3097 switch (predicate) {
3098 default: return false;
3099 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3100 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3101 case FCmpInst::FCMP_UNO: return true;
3102 }
3103}
3104
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003105bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3106 switch(predicate) {
3107 default: return false;
3108 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3109 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3110 }
3111}
3112
3113bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3114 switch(predicate) {
3115 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3116 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3117 default: return false;
3118 }
3119}
3120
3121
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003122//===----------------------------------------------------------------------===//
3123// SwitchInst Implementation
3124//===----------------------------------------------------------------------===//
3125
Chris Lattnerbaf00152010-11-17 05:41:46 +00003126void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3127 assert(Value && Default && NumReserved);
3128 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003129 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003130 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003131
Gabor Greif2d3024d2008-05-26 21:33:52 +00003132 OperandList[0] = Value;
3133 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003134}
3135
Chris Lattner2195fc42007-02-24 00:55:48 +00003136/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3137/// switch on and a default destination. The number of additional cases can
3138/// be specified here to make memory allocation more efficient. This
3139/// constructor can also autoinsert before another instruction.
3140SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3141 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003142 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3143 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003144 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003145}
3146
3147/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3148/// switch on and a default destination. The number of additional cases can
3149/// be specified here to make memory allocation more efficient. This
3150/// constructor also autoinserts at the end of the specified BasicBlock.
3151SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3152 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003153 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3154 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003155 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003156}
3157
Misha Brukmanb1c93172005-04-21 23:48:37 +00003158SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003159 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3160 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3161 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003162 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003163 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003164 OL[i] = InOL[i];
3165 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003166 }
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003167 TheSubsets = SI.TheSubsets;
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003168 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003169}
3170
Gordon Henriksen14a55692007-12-10 02:14:30 +00003171SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003172 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003173}
3174
3175
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003176/// addCase - Add an entry to the switch instruction...
3177///
Chris Lattner47ac1872005-02-24 05:32:09 +00003178void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003179 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00003180
3181 // FIXME: Currently we work with ConstantInt based cases.
3182 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003183 Mapping.add(IntItem::fromConstantInt(OnVal));
3184 IntegersSubset CaseRanges = Mapping.getCase();
3185 addCase(CaseRanges, Dest);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00003186}
3187
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003188void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003189 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003190 unsigned OpNo = NumOperands;
3191 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003192 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003193 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003194 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003195 NumOperands = OpNo+2;
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003196
3197 SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3198
3199 CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3200 Case.updateCaseValueOperand(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003201 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003202}
3203
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003204/// removeCase - This method removes the specified case and its successor
3205/// from the switch instruction.
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003206void SwitchInst::removeCase(CaseIt& i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003207 unsigned idx = i.getCaseIndex();
3208
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003209 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003210
3211 unsigned NumOps = getNumOperands();
3212 Use *OL = OperandList;
3213
Jay Foad14277722011-02-01 09:22:34 +00003214 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003215 if (2 + (idx + 1) * 2 != NumOps) {
3216 OL[2 + idx * 2] = OL[NumOps - 2];
3217 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003218 }
3219
3220 // Nuke the last value.
3221 OL[NumOps-2].set(0);
3222 OL[NumOps-2+1].set(0);
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003223
3224 // Do the same with TheCases collection:
3225 if (i.SubsetIt != --TheSubsets.end()) {
3226 *i.SubsetIt = TheSubsets.back();
3227 TheSubsets.pop_back();
3228 } else {
3229 TheSubsets.pop_back();
3230 i.SubsetIt = TheSubsets.end();
3231 }
3232
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003233 NumOperands = NumOps-2;
3234}
3235
Jay Foade98f29d2011-04-01 08:00:58 +00003236/// growOperands - grow operands - This grows the operand list in response
3237/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003238///
Jay Foade98f29d2011-04-01 08:00:58 +00003239void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003240 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003241 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003242
3243 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003244 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003245 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003246 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003247 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003248 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003249 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003250 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003251}
3252
3253
3254BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3255 return getSuccessor(idx);
3256}
3257unsigned SwitchInst::getNumSuccessorsV() const {
3258 return getNumSuccessors();
3259}
3260void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3261 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003262}
Chris Lattnerf22be932004-10-15 23:52:53 +00003263
Chris Lattner3ed871f2009-10-27 19:13:16 +00003264//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003265// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003266//===----------------------------------------------------------------------===//
3267
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003268void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003269 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003270 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003271 ReservedSpace = 1+NumDests;
3272 NumOperands = 1;
3273 OperandList = allocHungoffUses(ReservedSpace);
3274
3275 OperandList[0] = Address;
3276}
3277
3278
Jay Foade98f29d2011-04-01 08:00:58 +00003279/// growOperands - grow operands - This grows the operand list in response
3280/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003281///
Jay Foade98f29d2011-04-01 08:00:58 +00003282void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003283 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003284 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003285
3286 ReservedSpace = NumOps;
3287 Use *NewOps = allocHungoffUses(NumOps);
3288 Use *OldOps = OperandList;
3289 for (unsigned i = 0; i != e; ++i)
3290 NewOps[i] = OldOps[i];
3291 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003292 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003293}
3294
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003295IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3296 Instruction *InsertBefore)
3297: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003298 0, 0, InsertBefore) {
3299 init(Address, NumCases);
3300}
3301
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003302IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3303 BasicBlock *InsertAtEnd)
3304: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003305 0, 0, InsertAtEnd) {
3306 init(Address, NumCases);
3307}
3308
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003309IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3310 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003311 allocHungoffUses(IBI.getNumOperands()),
3312 IBI.getNumOperands()) {
3313 Use *OL = OperandList, *InOL = IBI.OperandList;
3314 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3315 OL[i] = InOL[i];
3316 SubclassOptionalData = IBI.SubclassOptionalData;
3317}
3318
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003319IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003320 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003321}
3322
3323/// addDestination - Add a destination.
3324///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003325void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003326 unsigned OpNo = NumOperands;
3327 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003328 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003329 // Initialize some new operands.
3330 assert(OpNo < ReservedSpace && "Growing didn't work!");
3331 NumOperands = OpNo+1;
3332 OperandList[OpNo] = DestBB;
3333}
3334
3335/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003336/// indirectbr instruction.
3337void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003338 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3339
3340 unsigned NumOps = getNumOperands();
3341 Use *OL = OperandList;
3342
3343 // Replace this value with the last one.
3344 OL[idx+1] = OL[NumOps-1];
3345
3346 // Nuke the last value.
3347 OL[NumOps-1].set(0);
3348 NumOperands = NumOps-1;
3349}
3350
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003351BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003352 return getSuccessor(idx);
3353}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003354unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003355 return getNumSuccessors();
3356}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003357void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003358 setSuccessor(idx, B);
3359}
3360
3361//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003362// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003363//===----------------------------------------------------------------------===//
3364
Chris Lattnerf22be932004-10-15 23:52:53 +00003365// Define these methods here so vtables don't get emitted into every translation
3366// unit that uses these classes.
3367
Devang Patel11cf3f42009-10-27 22:16:29 +00003368GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3369 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003370}
3371
Devang Patel11cf3f42009-10-27 22:16:29 +00003372BinaryOperator *BinaryOperator::clone_impl() const {
3373 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003374}
3375
Devang Patel11cf3f42009-10-27 22:16:29 +00003376FCmpInst* FCmpInst::clone_impl() const {
3377 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003378}
3379
Devang Patel11cf3f42009-10-27 22:16:29 +00003380ICmpInst* ICmpInst::clone_impl() const {
3381 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003382}
3383
Devang Patel11cf3f42009-10-27 22:16:29 +00003384ExtractValueInst *ExtractValueInst::clone_impl() const {
3385 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003386}
3387
Devang Patel11cf3f42009-10-27 22:16:29 +00003388InsertValueInst *InsertValueInst::clone_impl() const {
3389 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003390}
3391
Devang Patel11cf3f42009-10-27 22:16:29 +00003392AllocaInst *AllocaInst::clone_impl() const {
3393 return new AllocaInst(getAllocatedType(),
3394 (Value*)getOperand(0),
3395 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003396}
3397
Devang Patel11cf3f42009-10-27 22:16:29 +00003398LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003399 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3400 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003401}
3402
Devang Patel11cf3f42009-10-27 22:16:29 +00003403StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003404 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003405 getAlignment(), getOrdering(), getSynchScope());
3406
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003407}
3408
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003409AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3410 AtomicCmpXchgInst *Result =
3411 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3412 getOrdering(), getSynchScope());
3413 Result->setVolatile(isVolatile());
3414 return Result;
3415}
3416
3417AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3418 AtomicRMWInst *Result =
3419 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3420 getOrdering(), getSynchScope());
3421 Result->setVolatile(isVolatile());
3422 return Result;
3423}
3424
Eli Friedmanfee02c62011-07-25 23:16:38 +00003425FenceInst *FenceInst::clone_impl() const {
3426 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3427}
3428
Devang Patel11cf3f42009-10-27 22:16:29 +00003429TruncInst *TruncInst::clone_impl() const {
3430 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003431}
3432
Devang Patel11cf3f42009-10-27 22:16:29 +00003433ZExtInst *ZExtInst::clone_impl() const {
3434 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003435}
3436
Devang Patel11cf3f42009-10-27 22:16:29 +00003437SExtInst *SExtInst::clone_impl() const {
3438 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003439}
3440
Devang Patel11cf3f42009-10-27 22:16:29 +00003441FPTruncInst *FPTruncInst::clone_impl() const {
3442 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003443}
3444
Devang Patel11cf3f42009-10-27 22:16:29 +00003445FPExtInst *FPExtInst::clone_impl() const {
3446 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003447}
3448
Devang Patel11cf3f42009-10-27 22:16:29 +00003449UIToFPInst *UIToFPInst::clone_impl() const {
3450 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003451}
3452
Devang Patel11cf3f42009-10-27 22:16:29 +00003453SIToFPInst *SIToFPInst::clone_impl() const {
3454 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003455}
3456
Devang Patel11cf3f42009-10-27 22:16:29 +00003457FPToUIInst *FPToUIInst::clone_impl() const {
3458 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003459}
3460
Devang Patel11cf3f42009-10-27 22:16:29 +00003461FPToSIInst *FPToSIInst::clone_impl() const {
3462 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003463}
3464
Devang Patel11cf3f42009-10-27 22:16:29 +00003465PtrToIntInst *PtrToIntInst::clone_impl() const {
3466 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003467}
3468
Devang Patel11cf3f42009-10-27 22:16:29 +00003469IntToPtrInst *IntToPtrInst::clone_impl() const {
3470 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003471}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003472
Devang Patel11cf3f42009-10-27 22:16:29 +00003473BitCastInst *BitCastInst::clone_impl() const {
3474 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003475}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003476
Devang Patel11cf3f42009-10-27 22:16:29 +00003477CallInst *CallInst::clone_impl() const {
3478 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003479}
3480
Devang Patel11cf3f42009-10-27 22:16:29 +00003481SelectInst *SelectInst::clone_impl() const {
3482 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003483}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003484
Devang Patel11cf3f42009-10-27 22:16:29 +00003485VAArgInst *VAArgInst::clone_impl() const {
3486 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003487}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003488
Devang Patel11cf3f42009-10-27 22:16:29 +00003489ExtractElementInst *ExtractElementInst::clone_impl() const {
3490 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003491}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003492
Devang Patel11cf3f42009-10-27 22:16:29 +00003493InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003494 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003495}
3496
Devang Patel11cf3f42009-10-27 22:16:29 +00003497ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003498 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003499}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003500
Devang Patel11cf3f42009-10-27 22:16:29 +00003501PHINode *PHINode::clone_impl() const {
3502 return new PHINode(*this);
3503}
3504
Bill Wendlingfae14752011-08-12 20:24:12 +00003505LandingPadInst *LandingPadInst::clone_impl() const {
3506 return new LandingPadInst(*this);
3507}
3508
Devang Patel11cf3f42009-10-27 22:16:29 +00003509ReturnInst *ReturnInst::clone_impl() const {
3510 return new(getNumOperands()) ReturnInst(*this);
3511}
3512
3513BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003514 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003515}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003516
Devang Patel11cf3f42009-10-27 22:16:29 +00003517SwitchInst *SwitchInst::clone_impl() const {
3518 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003519}
3520
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003521IndirectBrInst *IndirectBrInst::clone_impl() const {
3522 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003523}
3524
3525
Devang Patel11cf3f42009-10-27 22:16:29 +00003526InvokeInst *InvokeInst::clone_impl() const {
3527 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003528}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003529
Bill Wendlingf891bf82011-07-31 06:30:59 +00003530ResumeInst *ResumeInst::clone_impl() const {
3531 return new(1) ResumeInst(*this);
3532}
3533
Devang Patel11cf3f42009-10-27 22:16:29 +00003534UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003535 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003536 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003537}