blob: abee7b741a55a697fce11f8e5d9ae19c0018c8fd [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000032User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
34 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000035 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000036 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037}
38
Gordon Henriksen14a55692007-12-10 02:14:30 +000039//===----------------------------------------------------------------------===//
40// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43// Out of line virtual method, so the vtable, etc has a home.
44TerminatorInst::~TerminatorInst() {
45}
46
Gabor Greiff6caff662008-05-10 08:32:32 +000047//===----------------------------------------------------------------------===//
48// UnaryInstruction Class
49//===----------------------------------------------------------------------===//
50
Gordon Henriksen14a55692007-12-10 02:14:30 +000051// Out of line virtual method, so the vtable, etc has a home.
52UnaryInstruction::~UnaryInstruction() {
53}
54
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000056// SelectInst Class
57//===----------------------------------------------------------------------===//
58
59/// areInvalidOperands - Return a string if the specified operands are invalid
60/// for a select operation, otherwise return null.
61const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
64
Chris Lattner229907c2011-07-18 04:54:35 +000065 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000066 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000067 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000068 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000069 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Chris Lattner88107952008-12-29 00:12:50 +000070 if (ET == 0)
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000075 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000076 return "select condition must be i1 or <n x i1>";
77 }
78 return 0;
79}
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000083// PHINode Class
84//===----------------------------------------------------------------------===//
85
86PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000089 ReservedSpace(PN.getNumOperands()) {
Jay Foad61ea0e42011-06-23 09:09:15 +000090 std::copy(PN.op_begin(), PN.op_end(), op_begin());
91 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000092 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000093}
94
Gordon Henriksen14a55692007-12-10 02:14:30 +000095PHINode::~PHINode() {
Jay Foadbbb91f22011-01-16 15:30:52 +000096 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097}
98
Jay Foad61ea0e42011-06-23 09:09:15 +000099Use *PHINode::allocHungoffUses(unsigned N) const {
100 // Allocate the array of Uses of the incoming values, followed by a pointer
101 // (with bottom bit set) to the User, followed by the array of pointers to
102 // the incoming basic blocks.
103 size_t size = N * sizeof(Use) + sizeof(Use::UserRef)
104 + N * sizeof(BasicBlock*);
105 Use *Begin = static_cast<Use*>(::operator new(size));
106 Use *End = Begin + N;
107 (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1);
108 return Use::initTags(Begin, End);
109}
110
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000111// removeIncomingValue - Remove an incoming value. This is useful if a
112// predecessor basic block is deleted.
113Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000114 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115
116 // Move everything after this operand down.
117 //
118 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000121 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
122 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000123
124 // Nuke the last value.
Jay Foad61ea0e42011-06-23 09:09:15 +0000125 Op<-1>().set(0);
126 --NumOperands;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000127
128 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000129 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000131 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000132 eraseFromParent();
133 }
134 return Removed;
135}
136
Jay Foade98f29d2011-04-01 08:00:58 +0000137/// growOperands - grow operands - This grows the operand list in response
138/// to a push_back style of operation. This grows the number of ops by 1.5
139/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000140///
Jay Foade98f29d2011-04-01 08:00:58 +0000141void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000142 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000143 unsigned NumOps = e + e / 2;
144 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
145
146 Use *OldOps = op_begin();
147 BasicBlock **OldBlocks = block_begin();
Jay Foade03c05c2011-06-20 14:38:01 +0000148
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 ReservedSpace = NumOps;
Jay Foad61ea0e42011-06-23 09:09:15 +0000150 OperandList = allocHungoffUses(ReservedSpace);
151
152 std::copy(OldOps, OldOps + e, op_begin());
153 std::copy(OldBlocks, OldBlocks + e, block_begin());
154
Jay Foadbbb91f22011-01-16 15:30:52 +0000155 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156}
157
Nate Begemanb3923212005-08-04 23:24:19 +0000158/// hasConstantValue - If the specified PHI node always merges together the same
159/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000160Value *PHINode::hasConstantValue() const {
161 // Exploit the fact that phi nodes always have at least one entry.
162 Value *ConstantValue = getIncomingValue(0);
163 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
164 if (getIncomingValue(i) != ConstantValue)
165 return 0; // Incoming values not all the same.
166 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000167}
168
Bill Wendling6c923bb2011-07-27 20:18:04 +0000169//===----------------------------------------------------------------------===//
170// LandingPadInst Implementation
171//===----------------------------------------------------------------------===//
172
Bill Wendling4f027232011-07-28 21:14:13 +0000173void LandingPadInst::init(Function *PersFn, unsigned NumReservedValues,
Bill Wendling6c923bb2011-07-27 20:18:04 +0000174 const Twine &NameStr) {
175 ReservedSpace = NumReservedValues;
176 NumOperands = 1;
177 OperandList = allocHungoffUses(ReservedSpace);
Bill Wendling4f027232011-07-28 21:14:13 +0000178 OperandList[0] = (Value*)PersFn;
Bill Wendling6c923bb2011-07-27 20:18:04 +0000179 setName(NameStr);
180}
181
182LandingPadInst::LandingPadInst(const LandingPadInst &LP)
183 : Instruction(LP.getType(), Instruction::LandingPad,
184 allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()),
185 ReservedSpace(LP.getNumOperands()) {
186 Use *OL = OperandList, *InOL = LP.OperandList;
187 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
188 OL[I] = InOL[I];
189
190 for (SmallVectorImpl<ClauseType>::const_iterator
191 I = LP.ClauseIdxs.begin(), E = LP.ClauseIdxs.end(); I != E; ++I)
192 ClauseIdxs.push_back(*I);
193
194 IsCleanup = LP.IsCleanup;
195 SubclassOptionalData = LP.SubclassOptionalData;
196}
197
198LandingPadInst::~LandingPadInst() {
199 dropHungoffUses();
200}
201
202/// growOperands - grow operands - This grows the operand list in response to a
203/// push_back style of operation. This grows the number of ops by 2 times.
204void LandingPadInst::growOperands() {
205 unsigned e = getNumOperands();
206 ReservedSpace = e * 2;
207
208 Use *NewOps = allocHungoffUses(ReservedSpace);
209 Use *OldOps = OperandList;
210 for (unsigned i = 0; i != e; ++i)
211 NewOps[i] = OldOps[i];
212
213 OperandList = NewOps;
214 Use::zap(OldOps, OldOps + e, true);
215}
216
Bill Wendlinga8f04e32011-07-28 02:15:52 +0000217void LandingPadInst::reserveClauses(unsigned Size) {
Bill Wendlingd8c1c1f2011-07-28 07:26:41 +0000218 unsigned e = getNumOperands();
219 if (ReservedSpace >= e + Size) return;
220 ReservedSpace = e + Size;
Bill Wendlinga8f04e32011-07-28 02:15:52 +0000221
222 Use *NewOps = allocHungoffUses(ReservedSpace);
223 Use *OldOps = OperandList;
224 for (unsigned i = 0; i != e; ++i)
225 NewOps[i] = OldOps[i];
226
227 OperandList = NewOps;
228 Use::zap(OldOps, OldOps + e, true);
229}
230
Bill Wendling4c934882011-07-28 02:27:12 +0000231void LandingPadInst::addClause(ClauseType CT, Constant *ClauseVal) {
Bill Wendling6c923bb2011-07-27 20:18:04 +0000232 unsigned OpNo = getNumOperands();
233 if (OpNo + 1 > ReservedSpace)
234 growOperands();
235 assert(OpNo < ReservedSpace && "Growing didn't work!");
236 ClauseIdxs.push_back(CT);
237 ++NumOperands;
Bill Wendling4c934882011-07-28 02:27:12 +0000238 OperandList[OpNo] = (Value*)ClauseVal;
Bill Wendling6c923bb2011-07-27 20:18:04 +0000239}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240
241//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242// CallInst Implementation
243//===----------------------------------------------------------------------===//
244
Gordon Henriksen14a55692007-12-10 02:14:30 +0000245CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246}
247
Jay Foad5bd375a2011-07-15 08:37:34 +0000248void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
249 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000250 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251
Jay Foad5bd375a2011-07-15 08:37:34 +0000252#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000253 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
255
Jay Foad5bd375a2011-07-15 08:37:34 +0000256 assert((Args.size() == FTy->getNumParams() ||
257 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000258 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000259
260 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000261 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000262 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000263 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000264#endif
265
266 std::copy(Args.begin(), Args.end(), op_begin());
267 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268}
269
Jay Foad5bd375a2011-07-15 08:37:34 +0000270void CallInst::init(Value *Func, const Twine &NameStr) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000271 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000272 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000273
Jay Foad5bd375a2011-07-15 08:37:34 +0000274#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000275 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
277
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000278 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000279#endif
280
281 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282}
283
Daniel Dunbar4975db62009-07-25 04:41:11 +0000284CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285 Instruction *InsertBefore)
286 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
287 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000288 Instruction::Call,
289 OperandTraits<CallInst>::op_end(this) - 1,
290 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000291 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000292}
293
Daniel Dunbar4975db62009-07-25 04:41:11 +0000294CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000295 BasicBlock *InsertAtEnd)
296 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
297 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000298 Instruction::Call,
299 OperandTraits<CallInst>::op_end(this) - 1,
300 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000301 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302}
303
Misha Brukmanb1c93172005-04-21 23:48:37 +0000304CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000305 : Instruction(CI.getType(), Instruction::Call,
306 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000307 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000308 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000309 setTailCall(CI.isTailCall());
310 setCallingConv(CI.getCallingConv());
311
Jay Foad5bd375a2011-07-15 08:37:34 +0000312 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000313 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314}
315
Devang Patel4c758ea2008-09-25 21:00:45 +0000316void CallInst::addAttribute(unsigned i, Attributes attr) {
317 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000318 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000319 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000320}
321
Devang Patel4c758ea2008-09-25 21:00:45 +0000322void CallInst::removeAttribute(unsigned i, Attributes attr) {
323 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000324 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000325 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000326}
327
Devang Patelba3fa6c2008-09-23 23:03:40 +0000328bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000329 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000330 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000331 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000332 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000333 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000334}
335
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000336/// IsConstantOne - Return true only if val is constant int 1
337static bool IsConstantOne(Value *val) {
338 assert(val && "IsConstantOne does not work with NULL val");
339 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
340}
341
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000342static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000343 BasicBlock *InsertAtEnd, Type *IntPtrTy,
344 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000345 Value *ArraySize, Function *MallocF,
346 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000347 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000348 "createMalloc needs either InsertBefore or InsertAtEnd");
349
350 // malloc(type) becomes:
351 // bitcast (i8* malloc(typeSize)) to type*
352 // malloc(type, arraySize) becomes:
353 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000354 if (!ArraySize)
355 ArraySize = ConstantInt::get(IntPtrTy, 1);
356 else if (ArraySize->getType() != IntPtrTy) {
357 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000358 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
359 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000360 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000361 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
362 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000363 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000364
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000365 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000366 if (IsConstantOne(AllocSize)) {
367 AllocSize = ArraySize; // Operand * 1 = Operand
368 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
369 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
370 false /*ZExt*/);
371 // Malloc arg is constant product of type size and array size
372 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
373 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000374 // Multiply type size by the array size...
375 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000376 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
377 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000378 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000379 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
380 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000381 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000382 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000383
Victor Hernandez788eaab2009-09-18 19:20:02 +0000384 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000385 // Create the call to Malloc.
386 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
387 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000388 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000389 Value *MallocFunc = MallocF;
390 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000391 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000392 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000393 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000394 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000395 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000396 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000397 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000398 Result = MCall;
399 if (Result->getType() != AllocPtrType)
400 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000401 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000402 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000403 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000404 Result = MCall;
405 if (Result->getType() != AllocPtrType) {
406 InsertAtEnd->getInstList().push_back(MCall);
407 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000408 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000409 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000410 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000411 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000412 if (Function *F = dyn_cast<Function>(MallocFunc)) {
413 MCall->setCallingConv(F->getCallingConv());
414 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
415 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000416 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000417
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000418 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000419}
420
421/// CreateMalloc - Generate the IR for a call to malloc:
422/// 1. Compute the malloc call's argument as the specified type's size,
423/// possibly multiplied by the array size if the array size is not
424/// constant 1.
425/// 2. Call malloc with that argument.
426/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000427Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000428 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000429 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000430 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000431 const Twine &Name) {
432 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000433 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000434}
435
436/// CreateMalloc - Generate the IR for a call to malloc:
437/// 1. Compute the malloc call's argument as the specified type's size,
438/// possibly multiplied by the array size if the array size is not
439/// constant 1.
440/// 2. Call malloc with that argument.
441/// 3. Bitcast the result of the malloc call to the specified type.
442/// Note: This function does not add the bitcast to the basic block, that is the
443/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000444Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000445 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000446 Value *AllocSize, Value *ArraySize,
447 Function *MallocF, const Twine &Name) {
448 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000449 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000450}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000451
Victor Hernandeze2971492009-10-24 04:23:03 +0000452static Instruction* createFree(Value* Source, Instruction *InsertBefore,
453 BasicBlock *InsertAtEnd) {
454 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
455 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000456 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000457 "Can not free something of nonpointer type!");
458
459 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
460 Module* M = BB->getParent()->getParent();
461
Chris Lattner229907c2011-07-18 04:54:35 +0000462 Type *VoidTy = Type::getVoidTy(M->getContext());
463 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000464 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000465 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000466 CallInst* Result = NULL;
467 Value *PtrCast = Source;
468 if (InsertBefore) {
469 if (Source->getType() != IntPtrTy)
470 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
471 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
472 } else {
473 if (Source->getType() != IntPtrTy)
474 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
475 Result = CallInst::Create(FreeFunc, PtrCast, "");
476 }
477 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000478 if (Function *F = dyn_cast<Function>(FreeFunc))
479 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000480
481 return Result;
482}
483
484/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000485Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
486 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000487}
488
489/// CreateFree - Generate the IR for a call to the builtin free function.
490/// Note: This function does not add the call to the basic block, that is the
491/// responsibility of the caller.
492Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
493 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
494 assert(FreeCall && "CreateFree did not create a CallInst");
495 return FreeCall;
496}
497
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000498//===----------------------------------------------------------------------===//
499// InvokeInst Implementation
500//===----------------------------------------------------------------------===//
501
502void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000503 ArrayRef<Value *> Args, const Twine &NameStr) {
504 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000505 Op<-3>() = Fn;
506 Op<-2>() = IfNormal;
507 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000508
509#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000510 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000511 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000512
Jay Foad5bd375a2011-07-15 08:37:34 +0000513 assert(((Args.size() == FTy->getNumParams()) ||
514 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000515 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000516
Jay Foad5bd375a2011-07-15 08:37:34 +0000517 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000518 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000519 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000520 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000521#endif
522
523 std::copy(Args.begin(), Args.end(), op_begin());
524 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000525}
526
Misha Brukmanb1c93172005-04-21 23:48:37 +0000527InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000528 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000529 OperandTraits<InvokeInst>::op_end(this)
530 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000531 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000532 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000533 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000534 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000535 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000536}
537
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000538BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
539 return getSuccessor(idx);
540}
541unsigned InvokeInst::getNumSuccessorsV() const {
542 return getNumSuccessors();
543}
544void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
545 return setSuccessor(idx, B);
546}
547
Devang Patelba3fa6c2008-09-23 23:03:40 +0000548bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000549 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000550 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000551 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000552 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000553 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000554}
555
Devang Patel4c758ea2008-09-25 21:00:45 +0000556void InvokeInst::addAttribute(unsigned i, Attributes attr) {
557 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000558 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000559 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000560}
561
Devang Patel4c758ea2008-09-25 21:00:45 +0000562void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
563 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000564 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000565 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000566}
567
Bill Wendlinga8f04e32011-07-28 02:15:52 +0000568LandingPadInst *InvokeInst::getLandingPad() const {
569 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
570}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000571
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000572//===----------------------------------------------------------------------===//
573// ReturnInst Implementation
574//===----------------------------------------------------------------------===//
575
Chris Lattner2195fc42007-02-24 00:55:48 +0000576ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000577 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000578 OperandTraits<ReturnInst>::op_end(this) -
579 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000580 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000581 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000582 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000583 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000584}
585
Owen Anderson55f1c092009-08-13 21:58:54 +0000586ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
587 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000588 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
589 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000590 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000591 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000592}
Owen Anderson55f1c092009-08-13 21:58:54 +0000593ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
594 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000595 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
596 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000597 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000598 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000599}
Owen Anderson55f1c092009-08-13 21:58:54 +0000600ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
601 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000602 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000603}
604
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605unsigned ReturnInst::getNumSuccessorsV() const {
606 return getNumSuccessors();
607}
608
Devang Patelae682fb2008-02-26 17:56:20 +0000609/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
610/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000611void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000612 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000613}
614
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000615BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000616 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000617 return 0;
618}
619
Devang Patel59643e52008-02-23 00:35:18 +0000620ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000621}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000622
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000623//===----------------------------------------------------------------------===//
624// UnwindInst Implementation
625//===----------------------------------------------------------------------===//
626
Owen Anderson55f1c092009-08-13 21:58:54 +0000627UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
628 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
629 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000630}
Owen Anderson55f1c092009-08-13 21:58:54 +0000631UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
632 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
633 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000634}
635
636
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000637unsigned UnwindInst::getNumSuccessorsV() const {
638 return getNumSuccessors();
639}
640
641void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000642 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000643}
644
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000645BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000646 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000647 return 0;
648}
649
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000650//===----------------------------------------------------------------------===//
Bill Wendling6c923bb2011-07-27 20:18:04 +0000651// ResumeInst Implementation
652//===----------------------------------------------------------------------===//
653
654ResumeInst::ResumeInst(const ResumeInst &RI)
655 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
656 OperandTraits<ResumeInst>::op_begin(this), 1) {
657 Op<0>() = RI.Op<0>();
658 SubclassOptionalData = RI.SubclassOptionalData;
659}
660
661ResumeInst::ResumeInst(LLVMContext &C, Value *Exn, Instruction *InsertBefore)
662 : TerminatorInst(Type::getVoidTy(C), Instruction::Resume,
663 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
664 Op<0>() = Exn;
665}
666
667ResumeInst::ResumeInst(LLVMContext &C, Value *Exn, BasicBlock *InsertAtEnd)
668 : TerminatorInst(Type::getVoidTy(C), Instruction::Resume,
669 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
670 Op<0>() = Exn;
671}
672
673unsigned ResumeInst::getNumSuccessorsV() const {
674 return getNumSuccessors();
675}
676
677void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
678 llvm_unreachable("ResumeInst has no successors!");
679}
680
681BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
682 llvm_unreachable("ResumeInst has no successors!");
683 return 0;
684}
685
686//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000687// UnreachableInst Implementation
688//===----------------------------------------------------------------------===//
689
Owen Anderson55f1c092009-08-13 21:58:54 +0000690UnreachableInst::UnreachableInst(LLVMContext &Context,
691 Instruction *InsertBefore)
692 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
693 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000694}
Owen Anderson55f1c092009-08-13 21:58:54 +0000695UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
696 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
697 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000698}
699
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000700unsigned UnreachableInst::getNumSuccessorsV() const {
701 return getNumSuccessors();
702}
703
704void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling6c923bb2011-07-27 20:18:04 +0000705 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706}
707
708BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling6c923bb2011-07-27 20:18:04 +0000709 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000710 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000711}
712
713//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000714// BranchInst Implementation
715//===----------------------------------------------------------------------===//
716
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717void BranchInst::AssertOK() {
718 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000719 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000721}
722
Chris Lattner2195fc42007-02-24 00:55:48 +0000723BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000724 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000725 OperandTraits<BranchInst>::op_end(this) - 1,
726 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000727 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000728 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000729}
730BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
731 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000732 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000733 OperandTraits<BranchInst>::op_end(this) - 3,
734 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000735 Op<-1>() = IfTrue;
736 Op<-2>() = IfFalse;
737 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000738#ifndef NDEBUG
739 AssertOK();
740#endif
741}
742
743BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000744 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000745 OperandTraits<BranchInst>::op_end(this) - 1,
746 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000747 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000748 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000749}
750
751BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
752 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000753 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000754 OperandTraits<BranchInst>::op_end(this) - 3,
755 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000756 Op<-1>() = IfTrue;
757 Op<-2>() = IfFalse;
758 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000759#ifndef NDEBUG
760 AssertOK();
761#endif
762}
763
764
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000765BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000766 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000767 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
768 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000769 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000770 if (BI.getNumOperands() != 1) {
771 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000772 Op<-3>() = BI.Op<-3>();
773 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000774 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000775 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776}
777
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000778BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
779 return getSuccessor(idx);
780}
781unsigned BranchInst::getNumSuccessorsV() const {
782 return getNumSuccessors();
783}
784void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
785 setSuccessor(idx, B);
786}
787
788
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000789//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000790// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000791//===----------------------------------------------------------------------===//
792
Owen Andersonb6b25302009-07-14 23:09:55 +0000793static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000794 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000795 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000796 else {
797 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000798 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000799 assert(Amt->getType()->isIntegerTy() &&
800 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000801 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000802 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803}
804
Chris Lattner229907c2011-07-18 04:54:35 +0000805AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000806 const Twine &Name, Instruction *InsertBefore)
807 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
808 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
809 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000810 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000811 setName(Name);
812}
813
Chris Lattner229907c2011-07-18 04:54:35 +0000814AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000815 const Twine &Name, BasicBlock *InsertAtEnd)
816 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
817 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
818 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000819 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000820 setName(Name);
821}
822
Chris Lattner229907c2011-07-18 04:54:35 +0000823AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000824 Instruction *InsertBefore)
825 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
826 getAISize(Ty->getContext(), 0), InsertBefore) {
827 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000828 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000829 setName(Name);
830}
831
Chris Lattner229907c2011-07-18 04:54:35 +0000832AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000833 BasicBlock *InsertAtEnd)
834 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
835 getAISize(Ty->getContext(), 0), InsertAtEnd) {
836 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000837 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000838 setName(Name);
839}
840
Chris Lattner229907c2011-07-18 04:54:35 +0000841AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000842 const Twine &Name, Instruction *InsertBefore)
843 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000844 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000845 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000846 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000847 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000848}
849
Chris Lattner229907c2011-07-18 04:54:35 +0000850AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000851 const Twine &Name, BasicBlock *InsertAtEnd)
852 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000853 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000854 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000855 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000856 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857}
858
Gordon Henriksen14a55692007-12-10 02:14:30 +0000859// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000860AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000861}
862
Victor Hernandez8acf2952009-10-23 21:09:37 +0000863void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000864 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000865 assert(Align <= MaximumAlignment &&
866 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000867 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000868 assert(getAlignment() == Align && "Alignment representation error!");
869}
870
Victor Hernandez8acf2952009-10-23 21:09:37 +0000871bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000872 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000873 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000874 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000875}
876
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000877Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000878 return getType()->getElementType();
879}
880
Chris Lattner8b291e62008-11-26 02:54:17 +0000881/// isStaticAlloca - Return true if this alloca is in the entry block of the
882/// function and is a constant size. If so, the code generator will fold it
883/// into the prolog/epilog code, so it is basically free.
884bool AllocaInst::isStaticAlloca() const {
885 // Must be constant size.
886 if (!isa<ConstantInt>(getArraySize())) return false;
887
888 // Must be in the entry block.
889 const BasicBlock *Parent = getParent();
890 return Parent == &Parent->getParent()->front();
891}
892
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000893//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000894// LoadInst Implementation
895//===----------------------------------------------------------------------===//
896
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000897void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000898 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000899 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900}
901
Daniel Dunbar4975db62009-07-25 04:41:11 +0000902LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000904 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000905 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000906 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000907 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000908 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000909}
910
Daniel Dunbar4975db62009-07-25 04:41:11 +0000911LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000912 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000913 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000914 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000915 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000917 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000918}
919
Daniel Dunbar4975db62009-07-25 04:41:11 +0000920LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000921 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000923 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000924 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000925 setAlignment(0);
926 AssertOK();
927 setName(Name);
928}
929
Daniel Dunbar4975db62009-07-25 04:41:11 +0000930LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000931 unsigned Align, Instruction *InsertBef)
932 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
933 Load, Ptr, InsertBef) {
934 setVolatile(isVolatile);
935 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000937 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000938}
939
Daniel Dunbar4975db62009-07-25 04:41:11 +0000940LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000941 unsigned Align, BasicBlock *InsertAE)
942 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
943 Load, Ptr, InsertAE) {
944 setVolatile(isVolatile);
945 setAlignment(Align);
946 AssertOK();
947 setName(Name);
948}
949
Daniel Dunbar4975db62009-07-25 04:41:11 +0000950LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000951 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000952 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000953 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000954 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000955 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000956 AssertOK();
957 setName(Name);
958}
959
Daniel Dunbar27096822009-08-11 18:11:15 +0000960
961
962LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
963 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
964 Load, Ptr, InsertBef) {
965 setVolatile(false);
966 setAlignment(0);
967 AssertOK();
968 if (Name && Name[0]) setName(Name);
969}
970
971LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
972 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
973 Load, Ptr, InsertAE) {
974 setVolatile(false);
975 setAlignment(0);
976 AssertOK();
977 if (Name && Name[0]) setName(Name);
978}
979
980LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
981 Instruction *InsertBef)
982: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
983 Load, Ptr, InsertBef) {
984 setVolatile(isVolatile);
985 setAlignment(0);
986 AssertOK();
987 if (Name && Name[0]) setName(Name);
988}
989
990LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
991 BasicBlock *InsertAE)
992 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
993 Load, Ptr, InsertAE) {
994 setVolatile(isVolatile);
995 setAlignment(0);
996 AssertOK();
997 if (Name && Name[0]) setName(Name);
998}
999
Christopher Lamb84485702007-04-22 19:24:39 +00001000void LoadInst::setAlignment(unsigned Align) {
1001 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001002 assert(Align <= MaximumAlignment &&
1003 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001004 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1005 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001006 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001007}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001008
1009//===----------------------------------------------------------------------===//
1010// StoreInst Implementation
1011//===----------------------------------------------------------------------===//
1012
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001013void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001014 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001015 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001016 "Ptr must have pointer type!");
1017 assert(getOperand(0)->getType() ==
1018 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001019 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001020}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001021
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001022
1023StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001024 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001025 OperandTraits<StoreInst>::op_begin(this),
1026 OperandTraits<StoreInst>::operands(this),
1027 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001028 Op<0>() = val;
1029 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001030 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001031 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001032 AssertOK();
1033}
1034
1035StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001036 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001037 OperandTraits<StoreInst>::op_begin(this),
1038 OperandTraits<StoreInst>::operands(this),
1039 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001040 Op<0>() = val;
1041 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001042 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001043 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001044 AssertOK();
1045}
1046
Misha Brukmanb1c93172005-04-21 23:48:37 +00001047StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001048 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001049 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001050 OperandTraits<StoreInst>::op_begin(this),
1051 OperandTraits<StoreInst>::operands(this),
1052 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001053 Op<0>() = val;
1054 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001055 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001056 setAlignment(0);
1057 AssertOK();
1058}
1059
1060StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1061 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001062 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001063 OperandTraits<StoreInst>::op_begin(this),
1064 OperandTraits<StoreInst>::operands(this),
1065 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001066 Op<0>() = val;
1067 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001068 setVolatile(isVolatile);
1069 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001070 AssertOK();
1071}
1072
Misha Brukmanb1c93172005-04-21 23:48:37 +00001073StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001074 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001075 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001076 OperandTraits<StoreInst>::op_begin(this),
1077 OperandTraits<StoreInst>::operands(this),
1078 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001079 Op<0>() = val;
1080 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001081 setVolatile(isVolatile);
1082 setAlignment(Align);
1083 AssertOK();
1084}
1085
1086StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001087 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001088 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001089 OperandTraits<StoreInst>::op_begin(this),
1090 OperandTraits<StoreInst>::operands(this),
1091 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001092 Op<0>() = val;
1093 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001094 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001095 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001096 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001097}
1098
Christopher Lamb84485702007-04-22 19:24:39 +00001099void StoreInst::setAlignment(unsigned Align) {
1100 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001101 assert(Align <= MaximumAlignment &&
1102 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001103 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1104 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001105 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001106}
1107
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001108//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001109// AtomicCmpXchgInst Implementation
1110//===----------------------------------------------------------------------===//
1111
1112void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1113 AtomicOrdering Ordering,
1114 SynchronizationScope SynchScope) {
1115 Op<0>() = Ptr;
1116 Op<1>() = Cmp;
1117 Op<2>() = NewVal;
1118 setOrdering(Ordering);
1119 setSynchScope(SynchScope);
1120
1121 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1122 "All operands must be non-null!");
1123 assert(getOperand(0)->getType()->isPointerTy() &&
1124 "Ptr must have pointer type!");
1125 assert(getOperand(1)->getType() ==
1126 cast<PointerType>(getOperand(0)->getType())->getElementType()
1127 && "Ptr must be a pointer to Cmp type!");
1128 assert(getOperand(2)->getType() ==
1129 cast<PointerType>(getOperand(0)->getType())->getElementType()
1130 && "Ptr must be a pointer to NewVal type!");
1131 assert(Ordering != NotAtomic &&
1132 "AtomicCmpXchg instructions must be atomic!");
1133}
1134
1135AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1136 AtomicOrdering Ordering,
1137 SynchronizationScope SynchScope,
1138 Instruction *InsertBefore)
1139 : Instruction(Cmp->getType(), AtomicCmpXchg,
1140 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1141 OperandTraits<AtomicCmpXchgInst>::operands(this),
1142 InsertBefore) {
1143 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1144}
1145
1146AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1147 AtomicOrdering Ordering,
1148 SynchronizationScope SynchScope,
1149 BasicBlock *InsertAtEnd)
1150 : Instruction(Cmp->getType(), AtomicCmpXchg,
1151 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1152 OperandTraits<AtomicCmpXchgInst>::operands(this),
1153 InsertAtEnd) {
1154 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1155}
1156
1157//===----------------------------------------------------------------------===//
1158// AtomicRMWInst Implementation
1159//===----------------------------------------------------------------------===//
1160
1161void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1162 AtomicOrdering Ordering,
1163 SynchronizationScope SynchScope) {
1164 Op<0>() = Ptr;
1165 Op<1>() = Val;
1166 setOperation(Operation);
1167 setOrdering(Ordering);
1168 setSynchScope(SynchScope);
1169
1170 assert(getOperand(0) && getOperand(1) &&
1171 "All operands must be non-null!");
1172 assert(getOperand(0)->getType()->isPointerTy() &&
1173 "Ptr must have pointer type!");
1174 assert(getOperand(1)->getType() ==
1175 cast<PointerType>(getOperand(0)->getType())->getElementType()
1176 && "Ptr must be a pointer to Val type!");
1177 assert(Ordering != NotAtomic &&
1178 "AtomicRMW instructions must be atomic!");
1179}
1180
1181AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1182 AtomicOrdering Ordering,
1183 SynchronizationScope SynchScope,
1184 Instruction *InsertBefore)
1185 : Instruction(Val->getType(), AtomicRMW,
1186 OperandTraits<AtomicRMWInst>::op_begin(this),
1187 OperandTraits<AtomicRMWInst>::operands(this),
1188 InsertBefore) {
1189 Init(Operation, Ptr, Val, Ordering, SynchScope);
1190}
1191
1192AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1193 AtomicOrdering Ordering,
1194 SynchronizationScope SynchScope,
1195 BasicBlock *InsertAtEnd)
1196 : Instruction(Val->getType(), AtomicRMW,
1197 OperandTraits<AtomicRMWInst>::op_begin(this),
1198 OperandTraits<AtomicRMWInst>::operands(this),
1199 InsertAtEnd) {
1200 Init(Operation, Ptr, Val, Ordering, SynchScope);
1201}
1202
1203//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001204// FenceInst Implementation
1205//===----------------------------------------------------------------------===//
1206
1207FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1208 SynchronizationScope SynchScope,
1209 Instruction *InsertBefore)
1210 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1211 setOrdering(Ordering);
1212 setSynchScope(SynchScope);
1213}
1214
1215FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1216 SynchronizationScope SynchScope,
1217 BasicBlock *InsertAtEnd)
1218 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1219 setOrdering(Ordering);
1220 setSynchScope(SynchScope);
1221}
1222
1223//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001224// GetElementPtrInst Implementation
1225//===----------------------------------------------------------------------===//
1226
Jay Foadd1b78492011-07-25 09:48:08 +00001227void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001228 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001229 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1230 OperandList[0] = Ptr;
1231 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001232 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001233}
1234
Gabor Greiff6caff662008-05-10 08:32:32 +00001235GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001236 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001237 OperandTraits<GetElementPtrInst>::op_end(this)
1238 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001239 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001240 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001241 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001242}
1243
Chris Lattner6090a422009-03-09 04:46:40 +00001244/// getIndexedType - Returns the type of the element that would be accessed with
1245/// a gep instruction with the specified parameters.
1246///
1247/// The Idxs pointer should point to a continuous piece of memory containing the
1248/// indices, either as Value* or uint64_t.
1249///
1250/// A null type is returned if the indices are invalid for the specified
1251/// pointer type.
1252///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001253template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001254static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Chris Lattner229907c2011-07-18 04:54:35 +00001255 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001256 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001257 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001258
Chris Lattner6090a422009-03-09 04:46:40 +00001259 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001260 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001261 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001262
1263 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001264 // it cannot be 'stepped over'.
1265 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001266 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001267
Dan Gohman1ecaf452008-05-31 00:58:22 +00001268 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001269 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001270 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001271 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001272 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001273 if (!CT->indexValid(Index)) return 0;
1274 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001275 }
Jay Foadd1b78492011-07-25 09:48:08 +00001276 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001277}
1278
Jay Foadd1b78492011-07-25 09:48:08 +00001279Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1280 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001281}
1282
Chris Lattner229907c2011-07-18 04:54:35 +00001283Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001284 ArrayRef<Constant *> IdxList) {
1285 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001286}
1287
Jay Foadd1b78492011-07-25 09:48:08 +00001288Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1289 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001290}
1291
Chris Lattner45f15572007-04-14 00:12:57 +00001292/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1293/// zeros. If so, the result pointer and the first operand have the same
1294/// value, just potentially different types.
1295bool GetElementPtrInst::hasAllZeroIndices() const {
1296 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1297 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1298 if (!CI->isZero()) return false;
1299 } else {
1300 return false;
1301 }
1302 }
1303 return true;
1304}
1305
Chris Lattner27058292007-04-27 20:35:56 +00001306/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1307/// constant integers. If so, the result pointer and the first operand have
1308/// a constant offset between them.
1309bool GetElementPtrInst::hasAllConstantIndices() const {
1310 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1311 if (!isa<ConstantInt>(getOperand(i)))
1312 return false;
1313 }
1314 return true;
1315}
1316
Dan Gohman1b849082009-09-07 23:54:19 +00001317void GetElementPtrInst::setIsInBounds(bool B) {
1318 cast<GEPOperator>(this)->setIsInBounds(B);
1319}
Chris Lattner45f15572007-04-14 00:12:57 +00001320
Nick Lewycky28a5f252009-09-27 21:33:04 +00001321bool GetElementPtrInst::isInBounds() const {
1322 return cast<GEPOperator>(this)->isInBounds();
1323}
1324
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001325//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001326// ExtractElementInst Implementation
1327//===----------------------------------------------------------------------===//
1328
1329ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001330 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001331 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001332 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001333 ExtractElement,
1334 OperandTraits<ExtractElementInst>::op_begin(this),
1335 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001336 assert(isValidOperands(Val, Index) &&
1337 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001338 Op<0>() = Val;
1339 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001340 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001341}
1342
1343ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001344 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001345 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001346 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001347 ExtractElement,
1348 OperandTraits<ExtractElementInst>::op_begin(this),
1349 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001350 assert(isValidOperands(Val, Index) &&
1351 "Invalid extractelement instruction operands!");
1352
Gabor Greif2d3024d2008-05-26 21:33:52 +00001353 Op<0>() = Val;
1354 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001355 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001356}
1357
Chris Lattner65511ff2006-10-05 06:24:58 +00001358
Chris Lattner54865b32006-04-08 04:05:48 +00001359bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001360 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001361 return false;
1362 return true;
1363}
1364
1365
Robert Bocchino23004482006-01-10 19:05:34 +00001366//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001367// InsertElementInst Implementation
1368//===----------------------------------------------------------------------===//
1369
Chris Lattner54865b32006-04-08 04:05:48 +00001370InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001371 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001372 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001373 : Instruction(Vec->getType(), InsertElement,
1374 OperandTraits<InsertElementInst>::op_begin(this),
1375 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001376 assert(isValidOperands(Vec, Elt, Index) &&
1377 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001378 Op<0>() = Vec;
1379 Op<1>() = Elt;
1380 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001381 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001382}
1383
Chris Lattner54865b32006-04-08 04:05:48 +00001384InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001385 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001386 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001387 : Instruction(Vec->getType(), InsertElement,
1388 OperandTraits<InsertElementInst>::op_begin(this),
1389 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001390 assert(isValidOperands(Vec, Elt, Index) &&
1391 "Invalid insertelement instruction operands!");
1392
Gabor Greif2d3024d2008-05-26 21:33:52 +00001393 Op<0>() = Vec;
1394 Op<1>() = Elt;
1395 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001396 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001397}
1398
Chris Lattner54865b32006-04-08 04:05:48 +00001399bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1400 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001401 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001402 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001403
Reid Spencerd84d35b2007-02-15 02:26:10 +00001404 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001405 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001406
Duncan Sands9dff9be2010-02-15 16:12:20 +00001407 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001408 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001409 return true;
1410}
1411
1412
Robert Bocchinoca27f032006-01-17 20:07:22 +00001413//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001414// ShuffleVectorInst Implementation
1415//===----------------------------------------------------------------------===//
1416
1417ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001418 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001419 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001420: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001421 cast<VectorType>(Mask->getType())->getNumElements()),
1422 ShuffleVector,
1423 OperandTraits<ShuffleVectorInst>::op_begin(this),
1424 OperandTraits<ShuffleVectorInst>::operands(this),
1425 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001426 assert(isValidOperands(V1, V2, Mask) &&
1427 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001428 Op<0>() = V1;
1429 Op<1>() = V2;
1430 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001431 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001432}
1433
1434ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001435 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001436 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001437: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1438 cast<VectorType>(Mask->getType())->getNumElements()),
1439 ShuffleVector,
1440 OperandTraits<ShuffleVectorInst>::op_begin(this),
1441 OperandTraits<ShuffleVectorInst>::operands(this),
1442 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001443 assert(isValidOperands(V1, V2, Mask) &&
1444 "Invalid shuffle vector instruction operands!");
1445
Gabor Greif2d3024d2008-05-26 21:33:52 +00001446 Op<0>() = V1;
1447 Op<1>() = V2;
1448 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001449 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001450}
1451
Mon P Wang25f01062008-11-10 04:46:22 +00001452bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001453 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001454 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001455 return false;
1456
Chris Lattner229907c2011-07-18 04:54:35 +00001457 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001458 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001459 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001460
1461 // Check to see if Mask is valid.
1462 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001463 VectorType *VTy = cast<VectorType>(V1->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001464 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1465 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1466 if (CI->uge(VTy->getNumElements()*2))
1467 return false;
1468 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1469 return false;
1470 }
1471 }
1472 }
1473 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1474 return false;
1475
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001476 return true;
1477}
1478
Chris Lattnerf724e342008-03-02 05:28:33 +00001479/// getMaskValue - Return the index from the shuffle mask for the specified
1480/// output result. This is either -1 if the element is undef or a number less
1481/// than 2*numelements.
1482int ShuffleVectorInst::getMaskValue(unsigned i) const {
1483 const Constant *Mask = cast<Constant>(getOperand(2));
1484 if (isa<UndefValue>(Mask)) return -1;
1485 if (isa<ConstantAggregateZero>(Mask)) return 0;
1486 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1487 assert(i < MaskCV->getNumOperands() && "Index out of range");
1488
1489 if (isa<UndefValue>(MaskCV->getOperand(i)))
1490 return -1;
1491 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1492}
1493
Dan Gohman12fce772008-05-15 19:50:34 +00001494//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001495// InsertValueInst Class
1496//===----------------------------------------------------------------------===//
1497
Jay Foad57aa6362011-07-13 10:26:04 +00001498void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1499 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001500 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001501
1502 // There's no fundamental reason why we require at least one index
1503 // (other than weirdness with &*IdxBegin being invalid; see
1504 // getelementptr's init routine for example). But there's no
1505 // present need to support it.
1506 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1507
1508 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001509 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001510 Op<0>() = Agg;
1511 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001512
Jay Foad57aa6362011-07-13 10:26:04 +00001513 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001514 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001515}
1516
1517InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001518 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001519 OperandTraits<InsertValueInst>::op_begin(this), 2),
1520 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001521 Op<0>() = IVI.getOperand(0);
1522 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001523 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001524}
1525
1526//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001527// ExtractValueInst Class
1528//===----------------------------------------------------------------------===//
1529
Jay Foad57aa6362011-07-13 10:26:04 +00001530void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001531 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001532
Jay Foad57aa6362011-07-13 10:26:04 +00001533 // There's no fundamental reason why we require at least one index.
1534 // But there's no present need to support it.
1535 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001536
Jay Foad57aa6362011-07-13 10:26:04 +00001537 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001538 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001539}
1540
1541ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001542 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001543 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001544 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001545}
1546
Dan Gohman12fce772008-05-15 19:50:34 +00001547// getIndexedType - Returns the type of the element that would be extracted
1548// with an extractvalue instruction with the specified parameters.
1549//
1550// A null type is returned if the indices are invalid for the specified
1551// pointer type.
1552//
Chris Lattner229907c2011-07-18 04:54:35 +00001553Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001554 ArrayRef<unsigned> Idxs) {
1555 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001556 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001557 // We can't use CompositeType::indexValid(Index) here.
1558 // indexValid() always returns true for arrays because getelementptr allows
1559 // out-of-bounds indices. Since we don't allow those for extractvalue and
1560 // insertvalue we need to check array indexing manually.
1561 // Since the only other types we can index into are struct types it's just
1562 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001563 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001564 if (Index >= AT->getNumElements())
1565 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001566 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001567 if (Index >= ST->getNumElements())
1568 return 0;
1569 } else {
1570 // Not a valid type to index into.
1571 return 0;
1572 }
1573
1574 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001575 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001576 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001577}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001578
1579//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001580// BinaryOperator Class
1581//===----------------------------------------------------------------------===//
1582
Chris Lattner2195fc42007-02-24 00:55:48 +00001583BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001584 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001585 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001586 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001587 OperandTraits<BinaryOperator>::op_begin(this),
1588 OperandTraits<BinaryOperator>::operands(this),
1589 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001590 Op<0>() = S1;
1591 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001592 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001593 setName(Name);
1594}
1595
1596BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001597 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001598 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001599 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001600 OperandTraits<BinaryOperator>::op_begin(this),
1601 OperandTraits<BinaryOperator>::operands(this),
1602 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001603 Op<0>() = S1;
1604 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001605 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001606 setName(Name);
1607}
1608
1609
1610void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001611 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001612 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001613 assert(LHS->getType() == RHS->getType() &&
1614 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001615#ifndef NDEBUG
1616 switch (iType) {
1617 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001618 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001619 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001620 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001621 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001622 "Tried to create an integer operation on a non-integer type!");
1623 break;
1624 case FAdd: case FSub:
1625 case FMul:
1626 assert(getType() == LHS->getType() &&
1627 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001628 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001629 "Tried to create a floating-point operation on a "
1630 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001631 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001632 case UDiv:
1633 case SDiv:
1634 assert(getType() == LHS->getType() &&
1635 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001636 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001637 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001638 "Incorrect operand type (not integer) for S/UDIV");
1639 break;
1640 case FDiv:
1641 assert(getType() == LHS->getType() &&
1642 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001643 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001644 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001645 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001646 case URem:
1647 case SRem:
1648 assert(getType() == LHS->getType() &&
1649 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001650 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001651 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001652 "Incorrect operand type (not integer) for S/UREM");
1653 break;
1654 case FRem:
1655 assert(getType() == LHS->getType() &&
1656 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001657 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001658 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001659 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001660 case Shl:
1661 case LShr:
1662 case AShr:
1663 assert(getType() == LHS->getType() &&
1664 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001665 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001666 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001667 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001668 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001669 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001670 case And: case Or:
1671 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001672 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001673 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001674 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001675 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001676 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001677 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001678 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001679 default:
1680 break;
1681 }
1682#endif
1683}
1684
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001685BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001686 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001687 Instruction *InsertBefore) {
1688 assert(S1->getType() == S2->getType() &&
1689 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001690 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001691}
1692
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001693BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001694 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001695 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001696 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001697 InsertAtEnd->getInstList().push_back(Res);
1698 return Res;
1699}
1700
Dan Gohman5476cfd2009-08-12 16:23:25 +00001701BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001702 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001703 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001704 return new BinaryOperator(Instruction::Sub,
1705 zero, Op,
1706 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001707}
1708
Dan Gohman5476cfd2009-08-12 16:23:25 +00001709BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001710 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001711 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001712 return new BinaryOperator(Instruction::Sub,
1713 zero, Op,
1714 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001715}
1716
Dan Gohman4ab44202009-12-18 02:58:50 +00001717BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1718 Instruction *InsertBefore) {
1719 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1720 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1721}
1722
1723BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1724 BasicBlock *InsertAtEnd) {
1725 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1726 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1727}
1728
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001729BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1730 Instruction *InsertBefore) {
1731 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1732 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1733}
1734
1735BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1736 BasicBlock *InsertAtEnd) {
1737 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1738 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1739}
1740
Dan Gohman5476cfd2009-08-12 16:23:25 +00001741BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001742 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001743 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001744 return new BinaryOperator(Instruction::FSub,
1745 zero, Op,
1746 Op->getType(), Name, InsertBefore);
1747}
1748
Dan Gohman5476cfd2009-08-12 16:23:25 +00001749BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001750 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001751 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001752 return new BinaryOperator(Instruction::FSub,
1753 zero, Op,
1754 Op->getType(), Name, InsertAtEnd);
1755}
1756
Dan Gohman5476cfd2009-08-12 16:23:25 +00001757BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001758 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001759 Constant *C;
Chris Lattner229907c2011-07-18 04:54:35 +00001760 if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001761 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001762 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001763 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001764 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001765 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001766 }
1767
1768 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001769 Op->getType(), Name, InsertBefore);
1770}
1771
Dan Gohman5476cfd2009-08-12 16:23:25 +00001772BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001773 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001774 Constant *AllOnes;
Chris Lattner229907c2011-07-18 04:54:35 +00001775 if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001776 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001777 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001778 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001779 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001780 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001781 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001782 }
1783
1784 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001785 Op->getType(), Name, InsertAtEnd);
1786}
1787
1788
1789// isConstantAllOnes - Helper function for several functions below
1790static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001791 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1792 return CI->isAllOnesValue();
1793 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1794 return CV->isAllOnesValue();
1795 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001796}
1797
Owen Andersonbb2501b2009-07-13 22:18:28 +00001798bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001799 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1800 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001801 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1802 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001803 return false;
1804}
1805
Owen Andersonbb2501b2009-07-13 22:18:28 +00001806bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001807 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1808 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001809 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001810 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001811 return false;
1812}
1813
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001814bool BinaryOperator::isNot(const Value *V) {
1815 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1816 return (Bop->getOpcode() == Instruction::Xor &&
1817 (isConstantAllOnes(Bop->getOperand(1)) ||
1818 isConstantAllOnes(Bop->getOperand(0))));
1819 return false;
1820}
1821
Chris Lattner2c7d1772005-04-24 07:28:37 +00001822Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001823 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001824}
1825
Chris Lattner2c7d1772005-04-24 07:28:37 +00001826const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1827 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001828}
1829
Dan Gohmana5b96452009-06-04 22:49:04 +00001830Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001831 return cast<BinaryOperator>(BinOp)->getOperand(1);
1832}
1833
1834const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1835 return getFNegArgument(const_cast<Value*>(BinOp));
1836}
1837
Chris Lattner2c7d1772005-04-24 07:28:37 +00001838Value *BinaryOperator::getNotArgument(Value *BinOp) {
1839 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1840 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1841 Value *Op0 = BO->getOperand(0);
1842 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001843 if (isConstantAllOnes(Op0)) return Op1;
1844
1845 assert(isConstantAllOnes(Op1));
1846 return Op0;
1847}
1848
Chris Lattner2c7d1772005-04-24 07:28:37 +00001849const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1850 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001851}
1852
1853
1854// swapOperands - Exchange the two operands to this instruction. This
1855// instruction is safe to use on any binary instruction and does not
1856// modify the semantics of the instruction. If the instruction is
1857// order dependent (SetLT f.e.) the opcode is changed.
1858//
1859bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001860 if (!isCommutative())
1861 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001862 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001863 return false;
1864}
1865
Dan Gohman1b849082009-09-07 23:54:19 +00001866void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1867 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1868}
1869
1870void BinaryOperator::setHasNoSignedWrap(bool b) {
1871 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1872}
1873
1874void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001875 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001876}
1877
Nick Lewycky28a5f252009-09-27 21:33:04 +00001878bool BinaryOperator::hasNoUnsignedWrap() const {
1879 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1880}
1881
1882bool BinaryOperator::hasNoSignedWrap() const {
1883 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1884}
1885
1886bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001887 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001888}
1889
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001890//===----------------------------------------------------------------------===//
1891// CastInst Class
1892//===----------------------------------------------------------------------===//
1893
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001894// Just determine if this cast only deals with integral->integral conversion.
1895bool CastInst::isIntegerCast() const {
1896 switch (getOpcode()) {
1897 default: return false;
1898 case Instruction::ZExt:
1899 case Instruction::SExt:
1900 case Instruction::Trunc:
1901 return true;
1902 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001903 return getOperand(0)->getType()->isIntegerTy() &&
1904 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001905 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001906}
1907
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001908bool CastInst::isLosslessCast() const {
1909 // Only BitCast can be lossless, exit fast if we're not BitCast
1910 if (getOpcode() != Instruction::BitCast)
1911 return false;
1912
1913 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00001914 Type* SrcTy = getOperand(0)->getType();
1915 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001916 if (SrcTy == DstTy)
1917 return true;
1918
Reid Spencer8d9336d2006-12-31 05:26:44 +00001919 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001920 if (SrcTy->isPointerTy())
1921 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001922 return false; // Other types have no identity values
1923}
1924
1925/// This function determines if the CastInst does not require any bits to be
1926/// changed in order to effect the cast. Essentially, it identifies cases where
1927/// no code gen is necessary for the cast, hence the name no-op cast. For
1928/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001929/// # bitcast i32* %x to i8*
1930/// # bitcast <2 x i32> %x to <4 x i16>
1931/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001932/// @brief Determine if the described cast is a no-op.
1933bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00001934 Type *SrcTy,
1935 Type *DestTy,
1936 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001937 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001938 default:
1939 assert(!"Invalid CastOp");
1940 case Instruction::Trunc:
1941 case Instruction::ZExt:
1942 case Instruction::SExt:
1943 case Instruction::FPTrunc:
1944 case Instruction::FPExt:
1945 case Instruction::UIToFP:
1946 case Instruction::SIToFP:
1947 case Instruction::FPToUI:
1948 case Instruction::FPToSI:
1949 return false; // These always modify bits
1950 case Instruction::BitCast:
1951 return true; // BitCast never modifies bits.
1952 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001953 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001954 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001955 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001956 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001957 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001958 }
1959}
1960
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001961/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00001962bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001963 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1964}
1965
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001966/// This function determines if a pair of casts can be eliminated and what
1967/// opcode should be used in the elimination. This assumes that there are two
1968/// instructions like this:
1969/// * %F = firstOpcode SrcTy %x to MidTy
1970/// * %S = secondOpcode MidTy %F to DstTy
1971/// The function returns a resultOpcode so these two casts can be replaced with:
1972/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1973/// If no such cast is permited, the function returns 0.
1974unsigned CastInst::isEliminableCastPair(
1975 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Chris Lattner229907c2011-07-18 04:54:35 +00001976 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001977{
1978 // Define the 144 possibilities for these two cast instructions. The values
1979 // in this matrix determine what to do in a given situation and select the
1980 // case in the switch below. The rows correspond to firstOp, the columns
1981 // correspond to secondOp. In looking at the table below, keep in mind
1982 // the following cast properties:
1983 //
1984 // Size Compare Source Destination
1985 // Operator Src ? Size Type Sign Type Sign
1986 // -------- ------------ ------------------- ---------------------
1987 // TRUNC > Integer Any Integral Any
1988 // ZEXT < Integral Unsigned Integer Any
1989 // SEXT < Integral Signed Integer Any
1990 // FPTOUI n/a FloatPt n/a Integral Unsigned
1991 // FPTOSI n/a FloatPt n/a Integral Signed
1992 // UITOFP n/a Integral Unsigned FloatPt n/a
1993 // SITOFP n/a Integral Signed FloatPt n/a
1994 // FPTRUNC > FloatPt n/a FloatPt n/a
1995 // FPEXT < FloatPt n/a FloatPt n/a
1996 // PTRTOINT n/a Pointer n/a Integral Unsigned
1997 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001998 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001999 //
2000 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002001 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2002 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002003 // of the produced value (we no longer know the top-part is all zeros).
2004 // Further this conversion is often much more expensive for typical hardware,
2005 // and causes issues when building libgcc. We disallow fptosi+sext for the
2006 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002007 const unsigned numCastOps =
2008 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2009 static const uint8_t CastResults[numCastOps][numCastOps] = {
2010 // T F F U S F F P I B -+
2011 // R Z S P P I I T P 2 N T |
2012 // U E E 2 2 2 2 R E I T C +- secondOp
2013 // N X X U S F F N X N 2 V |
2014 // C T T I I P P C T T P T -+
2015 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2016 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2017 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002018 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2019 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002020 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2021 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2022 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2023 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2024 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2025 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2026 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2027 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002028
2029 // If either of the casts are a bitcast from scalar to vector, disallow the
2030 // merging.
2031 if ((firstOp == Instruction::BitCast &&
2032 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2033 (secondOp == Instruction::BitCast &&
2034 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2035 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002036
2037 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2038 [secondOp-Instruction::CastOpsBegin];
2039 switch (ElimCase) {
2040 case 0:
2041 // categorically disallowed
2042 return 0;
2043 case 1:
2044 // allowed, use first cast's opcode
2045 return firstOp;
2046 case 2:
2047 // allowed, use second cast's opcode
2048 return secondOp;
2049 case 3:
2050 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002051 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002052 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002053 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002054 return firstOp;
2055 return 0;
2056 case 4:
2057 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002058 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002059 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060 return firstOp;
2061 return 0;
2062 case 5:
2063 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002064 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002065 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002066 return secondOp;
2067 return 0;
2068 case 6:
2069 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002070 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002071 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002072 return secondOp;
2073 return 0;
2074 case 7: {
2075 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002076 if (!IntPtrTy)
2077 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002078 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2079 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002080 if (MidSize >= PtrSize)
2081 return Instruction::BitCast;
2082 return 0;
2083 }
2084 case 8: {
2085 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2086 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2087 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002088 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2089 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002090 if (SrcSize == DstSize)
2091 return Instruction::BitCast;
2092 else if (SrcSize < DstSize)
2093 return firstOp;
2094 return secondOp;
2095 }
2096 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2097 return Instruction::ZExt;
2098 case 10:
2099 // fpext followed by ftrunc is allowed if the bit size returned to is
2100 // the same as the original, in which case its just a bitcast
2101 if (SrcTy == DstTy)
2102 return Instruction::BitCast;
2103 return 0; // If the types are not the same we can't eliminate it.
2104 case 11:
2105 // bitcast followed by ptrtoint is allowed as long as the bitcast
2106 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002107 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002108 return secondOp;
2109 return 0;
2110 case 12:
2111 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002112 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 return firstOp;
2114 return 0;
2115 case 13: {
2116 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002117 if (!IntPtrTy)
2118 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002119 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2120 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2121 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002122 if (SrcSize <= PtrSize && SrcSize == DstSize)
2123 return Instruction::BitCast;
2124 return 0;
2125 }
2126 case 99:
2127 // cast combination can't happen (error in input). This is for all cases
2128 // where the MidTy is not the same for the two cast instructions.
2129 assert(!"Invalid Cast Combination");
2130 return 0;
2131 default:
2132 assert(!"Error in CastResults table!!!");
2133 return 0;
2134 }
2135 return 0;
2136}
2137
Chris Lattner229907c2011-07-18 04:54:35 +00002138CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002139 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002140 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002141 // Construct and return the appropriate CastInst subclass
2142 switch (op) {
2143 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2144 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2145 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2146 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2147 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2148 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2149 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2150 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2151 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2152 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2153 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2154 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2155 default:
2156 assert(!"Invalid opcode provided");
2157 }
2158 return 0;
2159}
2160
Chris Lattner229907c2011-07-18 04:54:35 +00002161CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002162 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002163 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002164 // Construct and return the appropriate CastInst subclass
2165 switch (op) {
2166 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2167 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2168 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2169 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2170 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2171 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2172 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2173 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2174 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2175 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2176 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2177 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2178 default:
2179 assert(!"Invalid opcode provided");
2180 }
2181 return 0;
2182}
2183
Chris Lattner229907c2011-07-18 04:54:35 +00002184CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002185 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002186 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002187 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002188 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2189 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002190}
2191
Chris Lattner229907c2011-07-18 04:54:35 +00002192CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002193 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002194 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002195 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002196 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2197 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002198}
2199
Chris Lattner229907c2011-07-18 04:54:35 +00002200CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002201 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002202 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002203 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002204 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2205 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002206}
2207
Chris Lattner229907c2011-07-18 04:54:35 +00002208CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002209 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002210 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002211 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002212 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2213 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002214}
2215
Chris Lattner229907c2011-07-18 04:54:35 +00002216CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002217 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002218 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002219 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002220 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2221 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002222}
2223
Chris Lattner229907c2011-07-18 04:54:35 +00002224CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002225 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002226 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002227 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002228 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2229 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002230}
2231
Chris Lattner229907c2011-07-18 04:54:35 +00002232CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002233 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002234 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002235 assert(S->getType()->isPointerTy() && "Invalid cast");
2236 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002237 "Invalid cast");
2238
Duncan Sands9dff9be2010-02-15 16:12:20 +00002239 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002240 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2241 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002242}
2243
2244/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002245CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002246 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002247 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002248 assert(S->getType()->isPointerTy() && "Invalid cast");
2249 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002250 "Invalid cast");
2251
Duncan Sands9dff9be2010-02-15 16:12:20 +00002252 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002253 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2254 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002255}
2256
Chris Lattner229907c2011-07-18 04:54:35 +00002257CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002258 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002259 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002260 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002261 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002262 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2263 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002264 Instruction::CastOps opcode =
2265 (SrcBits == DstBits ? Instruction::BitCast :
2266 (SrcBits > DstBits ? Instruction::Trunc :
2267 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002268 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002269}
2270
Chris Lattner229907c2011-07-18 04:54:35 +00002271CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002272 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002273 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002274 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002275 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002276 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2277 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002278 Instruction::CastOps opcode =
2279 (SrcBits == DstBits ? Instruction::BitCast :
2280 (SrcBits > DstBits ? Instruction::Trunc :
2281 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002282 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002283}
2284
Chris Lattner229907c2011-07-18 04:54:35 +00002285CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002286 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002287 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002288 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002289 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002290 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2291 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002292 Instruction::CastOps opcode =
2293 (SrcBits == DstBits ? Instruction::BitCast :
2294 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002295 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002296}
2297
Chris Lattner229907c2011-07-18 04:54:35 +00002298CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002299 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002300 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002301 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002302 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002303 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2304 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002305 Instruction::CastOps opcode =
2306 (SrcBits == DstBits ? Instruction::BitCast :
2307 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002308 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002309}
2310
Duncan Sands55e50902008-01-06 10:12:28 +00002311// Check whether it is valid to call getCastOpcode for these types.
2312// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002313bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002314 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2315 return false;
2316
2317 if (SrcTy == DestTy)
2318 return true;
2319
Chris Lattner229907c2011-07-18 04:54:35 +00002320 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2321 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002322 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2323 // An element by element cast. Valid if casting the elements is valid.
2324 SrcTy = SrcVecTy->getElementType();
2325 DestTy = DestVecTy->getElementType();
2326 }
2327
Duncan Sands55e50902008-01-06 10:12:28 +00002328 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002329 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2330 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002331
2332 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002333 if (DestTy->isIntegerTy()) { // Casting to integral
2334 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002335 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002336 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002337 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002338 } else if (SrcTy->isVectorTy()) { // Casting from vector
2339 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002340 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002341 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002342 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002343 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2344 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002345 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002346 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002347 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002348 } else if (SrcTy->isVectorTy()) { // Casting from vector
2349 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002350 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002351 return false;
2352 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002353 } else if (DestTy->isVectorTy()) { // Casting to vector
2354 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002355 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002356 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002357 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002358 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002359 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002360 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002361 return false;
2362 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002363 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002364 if (SrcTy->isVectorTy()) {
2365 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002366 } else {
2367 return false;
2368 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002369 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002370 return false;
2371 }
2372}
2373
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374// Provide a way to get a "cast" where the cast opcode is inferred from the
2375// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002376// logic in the castIsValid function below. This axiom should hold:
2377// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2378// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002380// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002382CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002383 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2384 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385
Duncan Sands55e50902008-01-06 10:12:28 +00002386 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2387 "Only first class types are castable!");
2388
Duncan Sandsa8514532011-05-18 07:13:41 +00002389 if (SrcTy == DestTy)
2390 return BitCast;
2391
Chris Lattner229907c2011-07-18 04:54:35 +00002392 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2393 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002394 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2395 // An element by element cast. Find the appropriate opcode based on the
2396 // element types.
2397 SrcTy = SrcVecTy->getElementType();
2398 DestTy = DestVecTy->getElementType();
2399 }
2400
2401 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002402 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2403 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002404
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002406 if (DestTy->isIntegerTy()) { // Casting to integral
2407 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408 if (DestBits < SrcBits)
2409 return Trunc; // int -> smaller int
2410 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002411 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002412 return SExt; // signed -> SEXT
2413 else
2414 return ZExt; // unsigned -> ZEXT
2415 } else {
2416 return BitCast; // Same size, No-op cast
2417 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002418 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002419 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 return FPToSI; // FP -> sint
2421 else
2422 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002423 } else if (SrcTy->isVectorTy()) {
2424 assert(DestBits == SrcBits &&
2425 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426 return BitCast; // Same size, no-op cast
2427 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002428 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429 "Casting from a value that is not first-class type");
2430 return PtrToInt; // ptr -> int
2431 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002432 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2433 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002434 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 return SIToFP; // sint -> FP
2436 else
2437 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002438 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002439 if (DestBits < SrcBits) {
2440 return FPTrunc; // FP -> smaller FP
2441 } else if (DestBits > SrcBits) {
2442 return FPExt; // FP -> larger FP
2443 } else {
2444 return BitCast; // same size, no-op cast
2445 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002446 } else if (SrcTy->isVectorTy()) {
2447 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002448 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002449 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002451 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002452 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002453 } else if (DestTy->isVectorTy()) {
2454 assert(DestBits == SrcBits &&
2455 "Illegal cast to vector (wrong type or size)");
2456 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002457 } else if (DestTy->isPointerTy()) {
2458 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002460 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461 return IntToPtr; // int -> ptr
2462 } else {
2463 assert(!"Casting pointer to other than pointer or int");
2464 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002465 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002466 if (SrcTy->isVectorTy()) {
2467 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002468 return BitCast; // 64-bit vector to MMX
2469 } else {
2470 assert(!"Illegal cast to X86_MMX");
2471 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 } else {
2473 assert(!"Casting to type that is not first-class");
2474 }
2475
2476 // If we fall through to here we probably hit an assertion cast above
2477 // and assertions are not turned on. Anything we return is an error, so
2478 // BitCast is as good a choice as any.
2479 return BitCast;
2480}
2481
2482//===----------------------------------------------------------------------===//
2483// CastInst SubClass Constructors
2484//===----------------------------------------------------------------------===//
2485
2486/// Check that the construction parameters for a CastInst are correct. This
2487/// could be broken out into the separate constructors but it is useful to have
2488/// it in one place and to eliminate the redundant code for getting the sizes
2489/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002490bool
Chris Lattner229907c2011-07-18 04:54:35 +00002491CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492
2493 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002494 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002495 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2496 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497 return false;
2498
2499 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002500 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2501 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502
Duncan Sands7f646562011-05-18 09:21:57 +00002503 // If these are vector types, get the lengths of the vectors (using zero for
2504 // scalar types means that checking that vector lengths match also checks that
2505 // scalars are not being converted to vectors or vectors to scalars).
2506 unsigned SrcLength = SrcTy->isVectorTy() ?
2507 cast<VectorType>(SrcTy)->getNumElements() : 0;
2508 unsigned DstLength = DstTy->isVectorTy() ?
2509 cast<VectorType>(DstTy)->getNumElements() : 0;
2510
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511 // Switch on the opcode provided
2512 switch (op) {
2513 default: return false; // This is an input error
2514 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002515 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2516 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002518 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2519 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002521 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2522 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002523 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002524 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2525 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002527 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2528 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002531 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2532 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002534 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002535 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2536 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002538 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002540 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 case Instruction::BitCast:
2542 // BitCast implies a no-op cast of type only. No bits change.
2543 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002544 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545 return false;
2546
Duncan Sands55e50902008-01-06 10:12:28 +00002547 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002548 // these cases, the cast is okay if the source and destination bit widths
2549 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002550 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002551 }
2552}
2553
2554TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002555 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002557 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558}
2559
2560TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002561 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002563 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564}
2565
2566ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002567 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002569 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570}
2571
2572ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002573 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002575 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002576}
2577SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002578 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002580 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581}
2582
Jeff Cohencc08c832006-12-02 02:22:01 +00002583SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002584 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002586 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587}
2588
2589FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002590 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002592 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593}
2594
2595FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002596 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002598 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599}
2600
2601FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002602 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002604 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002605}
2606
2607FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002608 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002610 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611}
2612
2613UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002614 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002616 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617}
2618
2619UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002620 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002622 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623}
2624
2625SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002626 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002627) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002628 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002629}
2630
2631SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002632 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002633) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002634 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635}
2636
2637FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002638 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002639) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002640 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002641}
2642
2643FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002644 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002645) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002646 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647}
2648
2649FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002650 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002651) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002652 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653}
2654
2655FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002656 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002658 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659}
2660
2661PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002662 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002663) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002664 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665}
2666
2667PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002668 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002669) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002670 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671}
2672
2673IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002674 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002675) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002676 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677}
2678
2679IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002680 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002681) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002682 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002683}
2684
2685BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002686 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002687) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002688 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002689}
2690
2691BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002692 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002693) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002694 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002695}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002696
2697//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002698// CmpInst Classes
2699//===----------------------------------------------------------------------===//
2700
Chris Lattneraec33da2010-01-22 06:25:37 +00002701void CmpInst::Anchor() const {}
2702
Chris Lattner229907c2011-07-18 04:54:35 +00002703CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002704 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002705 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002706 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002707 OperandTraits<CmpInst>::op_begin(this),
2708 OperandTraits<CmpInst>::operands(this),
2709 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002710 Op<0>() = LHS;
2711 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002712 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002713 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002714}
Gabor Greiff6caff662008-05-10 08:32:32 +00002715
Chris Lattner229907c2011-07-18 04:54:35 +00002716CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002717 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002718 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002719 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002720 OperandTraits<CmpInst>::op_begin(this),
2721 OperandTraits<CmpInst>::operands(this),
2722 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002723 Op<0>() = LHS;
2724 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002725 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002726 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002727}
2728
2729CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002730CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002731 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002732 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002733 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002734 if (InsertBefore)
2735 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2736 S1, S2, Name);
2737 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002738 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002739 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002740 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002741
2742 if (InsertBefore)
2743 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2744 S1, S2, Name);
2745 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002746 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002747 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002748}
2749
2750CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002751CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002752 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002753 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002754 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2755 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002756 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002757 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2758 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002759}
2760
2761void CmpInst::swapOperands() {
2762 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2763 IC->swapOperands();
2764 else
2765 cast<FCmpInst>(this)->swapOperands();
2766}
2767
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002768bool CmpInst::isCommutative() const {
2769 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002770 return IC->isCommutative();
2771 return cast<FCmpInst>(this)->isCommutative();
2772}
2773
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002774bool CmpInst::isEquality() const {
2775 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002776 return IC->isEquality();
2777 return cast<FCmpInst>(this)->isEquality();
2778}
2779
2780
Dan Gohman4e724382008-05-31 02:47:54 +00002781CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002782 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002783 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002784 case ICMP_EQ: return ICMP_NE;
2785 case ICMP_NE: return ICMP_EQ;
2786 case ICMP_UGT: return ICMP_ULE;
2787 case ICMP_ULT: return ICMP_UGE;
2788 case ICMP_UGE: return ICMP_ULT;
2789 case ICMP_ULE: return ICMP_UGT;
2790 case ICMP_SGT: return ICMP_SLE;
2791 case ICMP_SLT: return ICMP_SGE;
2792 case ICMP_SGE: return ICMP_SLT;
2793 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002794
Dan Gohman4e724382008-05-31 02:47:54 +00002795 case FCMP_OEQ: return FCMP_UNE;
2796 case FCMP_ONE: return FCMP_UEQ;
2797 case FCMP_OGT: return FCMP_ULE;
2798 case FCMP_OLT: return FCMP_UGE;
2799 case FCMP_OGE: return FCMP_ULT;
2800 case FCMP_OLE: return FCMP_UGT;
2801 case FCMP_UEQ: return FCMP_ONE;
2802 case FCMP_UNE: return FCMP_OEQ;
2803 case FCMP_UGT: return FCMP_OLE;
2804 case FCMP_ULT: return FCMP_OGE;
2805 case FCMP_UGE: return FCMP_OLT;
2806 case FCMP_ULE: return FCMP_OGT;
2807 case FCMP_ORD: return FCMP_UNO;
2808 case FCMP_UNO: return FCMP_ORD;
2809 case FCMP_TRUE: return FCMP_FALSE;
2810 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002811 }
2812}
2813
Reid Spencer266e42b2006-12-23 06:05:41 +00002814ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2815 switch (pred) {
2816 default: assert(! "Unknown icmp predicate!");
2817 case ICMP_EQ: case ICMP_NE:
2818 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2819 return pred;
2820 case ICMP_UGT: return ICMP_SGT;
2821 case ICMP_ULT: return ICMP_SLT;
2822 case ICMP_UGE: return ICMP_SGE;
2823 case ICMP_ULE: return ICMP_SLE;
2824 }
2825}
2826
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002827ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2828 switch (pred) {
2829 default: assert(! "Unknown icmp predicate!");
2830 case ICMP_EQ: case ICMP_NE:
2831 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2832 return pred;
2833 case ICMP_SGT: return ICMP_UGT;
2834 case ICMP_SLT: return ICMP_ULT;
2835 case ICMP_SGE: return ICMP_UGE;
2836 case ICMP_SLE: return ICMP_ULE;
2837 }
2838}
2839
Reid Spencer0286bc12007-02-28 22:00:54 +00002840/// Initialize a set of values that all satisfy the condition with C.
2841///
2842ConstantRange
2843ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2844 APInt Lower(C);
2845 APInt Upper(C);
2846 uint32_t BitWidth = C.getBitWidth();
2847 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002848 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002849 case ICmpInst::ICMP_EQ: Upper++; break;
2850 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002851 case ICmpInst::ICMP_ULT:
2852 Lower = APInt::getMinValue(BitWidth);
2853 // Check for an empty-set condition.
2854 if (Lower == Upper)
2855 return ConstantRange(BitWidth, /*isFullSet=*/false);
2856 break;
2857 case ICmpInst::ICMP_SLT:
2858 Lower = APInt::getSignedMinValue(BitWidth);
2859 // Check for an empty-set condition.
2860 if (Lower == Upper)
2861 return ConstantRange(BitWidth, /*isFullSet=*/false);
2862 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002863 case ICmpInst::ICMP_UGT:
2864 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002865 // Check for an empty-set condition.
2866 if (Lower == Upper)
2867 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002868 break;
2869 case ICmpInst::ICMP_SGT:
2870 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002871 // Check for an empty-set condition.
2872 if (Lower == Upper)
2873 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002874 break;
2875 case ICmpInst::ICMP_ULE:
2876 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002877 // Check for a full-set condition.
2878 if (Lower == Upper)
2879 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002880 break;
2881 case ICmpInst::ICMP_SLE:
2882 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002883 // Check for a full-set condition.
2884 if (Lower == Upper)
2885 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002886 break;
2887 case ICmpInst::ICMP_UGE:
2888 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002889 // Check for a full-set condition.
2890 if (Lower == Upper)
2891 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002892 break;
2893 case ICmpInst::ICMP_SGE:
2894 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002895 // Check for a full-set condition.
2896 if (Lower == Upper)
2897 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002898 break;
2899 }
2900 return ConstantRange(Lower, Upper);
2901}
2902
Dan Gohman4e724382008-05-31 02:47:54 +00002903CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002904 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002905 default: assert(!"Unknown cmp predicate!");
2906 case ICMP_EQ: case ICMP_NE:
2907 return pred;
2908 case ICMP_SGT: return ICMP_SLT;
2909 case ICMP_SLT: return ICMP_SGT;
2910 case ICMP_SGE: return ICMP_SLE;
2911 case ICMP_SLE: return ICMP_SGE;
2912 case ICMP_UGT: return ICMP_ULT;
2913 case ICMP_ULT: return ICMP_UGT;
2914 case ICMP_UGE: return ICMP_ULE;
2915 case ICMP_ULE: return ICMP_UGE;
2916
Reid Spencerd9436b62006-11-20 01:22:35 +00002917 case FCMP_FALSE: case FCMP_TRUE:
2918 case FCMP_OEQ: case FCMP_ONE:
2919 case FCMP_UEQ: case FCMP_UNE:
2920 case FCMP_ORD: case FCMP_UNO:
2921 return pred;
2922 case FCMP_OGT: return FCMP_OLT;
2923 case FCMP_OLT: return FCMP_OGT;
2924 case FCMP_OGE: return FCMP_OLE;
2925 case FCMP_OLE: return FCMP_OGE;
2926 case FCMP_UGT: return FCMP_ULT;
2927 case FCMP_ULT: return FCMP_UGT;
2928 case FCMP_UGE: return FCMP_ULE;
2929 case FCMP_ULE: return FCMP_UGE;
2930 }
2931}
2932
Reid Spencer266e42b2006-12-23 06:05:41 +00002933bool CmpInst::isUnsigned(unsigned short predicate) {
2934 switch (predicate) {
2935 default: return false;
2936 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2937 case ICmpInst::ICMP_UGE: return true;
2938 }
2939}
2940
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002941bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002942 switch (predicate) {
2943 default: return false;
2944 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2945 case ICmpInst::ICMP_SGE: return true;
2946 }
2947}
2948
2949bool CmpInst::isOrdered(unsigned short predicate) {
2950 switch (predicate) {
2951 default: return false;
2952 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2953 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2954 case FCmpInst::FCMP_ORD: return true;
2955 }
2956}
2957
2958bool CmpInst::isUnordered(unsigned short predicate) {
2959 switch (predicate) {
2960 default: return false;
2961 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2962 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2963 case FCmpInst::FCMP_UNO: return true;
2964 }
2965}
2966
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002967bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2968 switch(predicate) {
2969 default: return false;
2970 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2971 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2972 }
2973}
2974
2975bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2976 switch(predicate) {
2977 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2978 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2979 default: return false;
2980 }
2981}
2982
2983
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002984//===----------------------------------------------------------------------===//
2985// SwitchInst Implementation
2986//===----------------------------------------------------------------------===//
2987
Chris Lattnerbaf00152010-11-17 05:41:46 +00002988void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
2989 assert(Value && Default && NumReserved);
2990 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002991 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002992 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002993
Gabor Greif2d3024d2008-05-26 21:33:52 +00002994 OperandList[0] = Value;
2995 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002996}
2997
Chris Lattner2195fc42007-02-24 00:55:48 +00002998/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2999/// switch on and a default destination. The number of additional cases can
3000/// be specified here to make memory allocation more efficient. This
3001/// constructor can also autoinsert before another instruction.
3002SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3003 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003004 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3005 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003006 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003007}
3008
3009/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3010/// switch on and a default destination. The number of additional cases can
3011/// be specified here to make memory allocation more efficient. This
3012/// constructor also autoinserts at the end of the specified BasicBlock.
3013SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3014 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003015 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3016 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003017 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003018}
3019
Misha Brukmanb1c93172005-04-21 23:48:37 +00003020SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003021 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3022 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3023 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003024 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003025 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003026 OL[i] = InOL[i];
3027 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003028 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003029 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003030}
3031
Gordon Henriksen14a55692007-12-10 02:14:30 +00003032SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003033 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003034}
3035
3036
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003037/// addCase - Add an entry to the switch instruction...
3038///
Chris Lattner47ac1872005-02-24 05:32:09 +00003039void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003040 unsigned OpNo = NumOperands;
3041 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003042 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003043 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003044 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003045 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003046 OperandList[OpNo] = OnVal;
3047 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003048}
3049
3050/// removeCase - This method removes the specified successor from the switch
3051/// instruction. Note that this cannot be used to remove the default
3052/// destination (successor #0).
3053///
3054void SwitchInst::removeCase(unsigned idx) {
3055 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003056 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3057
3058 unsigned NumOps = getNumOperands();
3059 Use *OL = OperandList;
3060
Jay Foad14277722011-02-01 09:22:34 +00003061 // Overwrite this case with the end of the list.
3062 if ((idx + 1) * 2 != NumOps) {
3063 OL[idx * 2] = OL[NumOps - 2];
3064 OL[idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003065 }
3066
3067 // Nuke the last value.
3068 OL[NumOps-2].set(0);
3069 OL[NumOps-2+1].set(0);
3070 NumOperands = NumOps-2;
3071}
3072
Jay Foade98f29d2011-04-01 08:00:58 +00003073/// growOperands - grow operands - This grows the operand list in response
3074/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003075///
Jay Foade98f29d2011-04-01 08:00:58 +00003076void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003077 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003078 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003079
3080 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003081 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003082 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003083 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003084 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003085 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003086 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003087 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003088}
3089
3090
3091BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3092 return getSuccessor(idx);
3093}
3094unsigned SwitchInst::getNumSuccessorsV() const {
3095 return getNumSuccessors();
3096}
3097void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3098 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003099}
Chris Lattnerf22be932004-10-15 23:52:53 +00003100
Chris Lattner3ed871f2009-10-27 19:13:16 +00003101//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003102// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003103//===----------------------------------------------------------------------===//
3104
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003105void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003106 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003107 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003108 ReservedSpace = 1+NumDests;
3109 NumOperands = 1;
3110 OperandList = allocHungoffUses(ReservedSpace);
3111
3112 OperandList[0] = Address;
3113}
3114
3115
Jay Foade98f29d2011-04-01 08:00:58 +00003116/// growOperands - grow operands - This grows the operand list in response
3117/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003118///
Jay Foade98f29d2011-04-01 08:00:58 +00003119void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003120 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003121 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003122
3123 ReservedSpace = NumOps;
3124 Use *NewOps = allocHungoffUses(NumOps);
3125 Use *OldOps = OperandList;
3126 for (unsigned i = 0; i != e; ++i)
3127 NewOps[i] = OldOps[i];
3128 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003129 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003130}
3131
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003132IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3133 Instruction *InsertBefore)
3134: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003135 0, 0, InsertBefore) {
3136 init(Address, NumCases);
3137}
3138
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003139IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3140 BasicBlock *InsertAtEnd)
3141: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003142 0, 0, InsertAtEnd) {
3143 init(Address, NumCases);
3144}
3145
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003146IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3147 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003148 allocHungoffUses(IBI.getNumOperands()),
3149 IBI.getNumOperands()) {
3150 Use *OL = OperandList, *InOL = IBI.OperandList;
3151 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3152 OL[i] = InOL[i];
3153 SubclassOptionalData = IBI.SubclassOptionalData;
3154}
3155
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003156IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003157 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003158}
3159
3160/// addDestination - Add a destination.
3161///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003162void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003163 unsigned OpNo = NumOperands;
3164 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003165 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003166 // Initialize some new operands.
3167 assert(OpNo < ReservedSpace && "Growing didn't work!");
3168 NumOperands = OpNo+1;
3169 OperandList[OpNo] = DestBB;
3170}
3171
3172/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003173/// indirectbr instruction.
3174void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003175 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3176
3177 unsigned NumOps = getNumOperands();
3178 Use *OL = OperandList;
3179
3180 // Replace this value with the last one.
3181 OL[idx+1] = OL[NumOps-1];
3182
3183 // Nuke the last value.
3184 OL[NumOps-1].set(0);
3185 NumOperands = NumOps-1;
3186}
3187
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003188BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003189 return getSuccessor(idx);
3190}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003191unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003192 return getNumSuccessors();
3193}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003194void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003195 setSuccessor(idx, B);
3196}
3197
3198//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003199// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003200//===----------------------------------------------------------------------===//
3201
Chris Lattnerf22be932004-10-15 23:52:53 +00003202// Define these methods here so vtables don't get emitted into every translation
3203// unit that uses these classes.
3204
Devang Patel11cf3f42009-10-27 22:16:29 +00003205GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3206 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003207}
3208
Devang Patel11cf3f42009-10-27 22:16:29 +00003209BinaryOperator *BinaryOperator::clone_impl() const {
3210 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003211}
3212
Devang Patel11cf3f42009-10-27 22:16:29 +00003213FCmpInst* FCmpInst::clone_impl() const {
3214 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003215}
3216
Devang Patel11cf3f42009-10-27 22:16:29 +00003217ICmpInst* ICmpInst::clone_impl() const {
3218 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003219}
3220
Devang Patel11cf3f42009-10-27 22:16:29 +00003221ExtractValueInst *ExtractValueInst::clone_impl() const {
3222 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003223}
3224
Devang Patel11cf3f42009-10-27 22:16:29 +00003225InsertValueInst *InsertValueInst::clone_impl() const {
3226 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003227}
3228
Devang Patel11cf3f42009-10-27 22:16:29 +00003229AllocaInst *AllocaInst::clone_impl() const {
3230 return new AllocaInst(getAllocatedType(),
3231 (Value*)getOperand(0),
3232 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003233}
3234
Devang Patel11cf3f42009-10-27 22:16:29 +00003235LoadInst *LoadInst::clone_impl() const {
3236 return new LoadInst(getOperand(0),
3237 Twine(), isVolatile(),
3238 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003239}
3240
Devang Patel11cf3f42009-10-27 22:16:29 +00003241StoreInst *StoreInst::clone_impl() const {
3242 return new StoreInst(getOperand(0), getOperand(1),
3243 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003244}
3245
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003246AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3247 AtomicCmpXchgInst *Result =
3248 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3249 getOrdering(), getSynchScope());
3250 Result->setVolatile(isVolatile());
3251 return Result;
3252}
3253
3254AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3255 AtomicRMWInst *Result =
3256 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3257 getOrdering(), getSynchScope());
3258 Result->setVolatile(isVolatile());
3259 return Result;
3260}
3261
Eli Friedmanfee02c62011-07-25 23:16:38 +00003262FenceInst *FenceInst::clone_impl() const {
3263 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3264}
3265
Devang Patel11cf3f42009-10-27 22:16:29 +00003266TruncInst *TruncInst::clone_impl() const {
3267 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003268}
3269
Devang Patel11cf3f42009-10-27 22:16:29 +00003270ZExtInst *ZExtInst::clone_impl() const {
3271 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003272}
3273
Devang Patel11cf3f42009-10-27 22:16:29 +00003274SExtInst *SExtInst::clone_impl() const {
3275 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003276}
3277
Devang Patel11cf3f42009-10-27 22:16:29 +00003278FPTruncInst *FPTruncInst::clone_impl() const {
3279 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003280}
3281
Devang Patel11cf3f42009-10-27 22:16:29 +00003282FPExtInst *FPExtInst::clone_impl() const {
3283 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003284}
3285
Devang Patel11cf3f42009-10-27 22:16:29 +00003286UIToFPInst *UIToFPInst::clone_impl() const {
3287 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003288}
3289
Devang Patel11cf3f42009-10-27 22:16:29 +00003290SIToFPInst *SIToFPInst::clone_impl() const {
3291 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003292}
3293
Devang Patel11cf3f42009-10-27 22:16:29 +00003294FPToUIInst *FPToUIInst::clone_impl() const {
3295 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003296}
3297
Devang Patel11cf3f42009-10-27 22:16:29 +00003298FPToSIInst *FPToSIInst::clone_impl() const {
3299 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003300}
3301
Devang Patel11cf3f42009-10-27 22:16:29 +00003302PtrToIntInst *PtrToIntInst::clone_impl() const {
3303 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003304}
3305
Devang Patel11cf3f42009-10-27 22:16:29 +00003306IntToPtrInst *IntToPtrInst::clone_impl() const {
3307 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003308}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003309
Devang Patel11cf3f42009-10-27 22:16:29 +00003310BitCastInst *BitCastInst::clone_impl() const {
3311 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003312}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003313
Devang Patel11cf3f42009-10-27 22:16:29 +00003314CallInst *CallInst::clone_impl() const {
3315 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003316}
3317
Devang Patel11cf3f42009-10-27 22:16:29 +00003318SelectInst *SelectInst::clone_impl() const {
3319 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003320}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003321
Devang Patel11cf3f42009-10-27 22:16:29 +00003322VAArgInst *VAArgInst::clone_impl() const {
3323 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003324}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003325
Devang Patel11cf3f42009-10-27 22:16:29 +00003326ExtractElementInst *ExtractElementInst::clone_impl() const {
3327 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003328}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003329
Devang Patel11cf3f42009-10-27 22:16:29 +00003330InsertElementInst *InsertElementInst::clone_impl() const {
3331 return InsertElementInst::Create(getOperand(0),
3332 getOperand(1),
3333 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003334}
3335
Devang Patel11cf3f42009-10-27 22:16:29 +00003336ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3337 return new ShuffleVectorInst(getOperand(0),
3338 getOperand(1),
3339 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003340}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003341
Devang Patel11cf3f42009-10-27 22:16:29 +00003342PHINode *PHINode::clone_impl() const {
3343 return new PHINode(*this);
3344}
3345
Bill Wendling6c923bb2011-07-27 20:18:04 +00003346LandingPadInst *LandingPadInst::clone_impl() const {
3347 return new LandingPadInst(*this);
3348}
3349
Devang Patel11cf3f42009-10-27 22:16:29 +00003350ReturnInst *ReturnInst::clone_impl() const {
3351 return new(getNumOperands()) ReturnInst(*this);
3352}
3353
3354BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003355 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003356}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003357
Devang Patel11cf3f42009-10-27 22:16:29 +00003358SwitchInst *SwitchInst::clone_impl() const {
3359 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003360}
3361
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003362IndirectBrInst *IndirectBrInst::clone_impl() const {
3363 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003364}
3365
3366
Devang Patel11cf3f42009-10-27 22:16:29 +00003367InvokeInst *InvokeInst::clone_impl() const {
3368 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003369}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003370
Bill Wendling6c923bb2011-07-27 20:18:04 +00003371ResumeInst *ResumeInst::clone_impl() const {
3372 return new(1) ResumeInst(*this);
3373}
3374
Devang Patel11cf3f42009-10-27 22:16:29 +00003375UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003376 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003377 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003378}
3379
Devang Patel11cf3f42009-10-27 22:16:29 +00003380UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003381 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003382 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003383}