blob: 9b700451beee05c5c93bb85553dde531edaff27e [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000032User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
34 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000035 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000036 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037}
38
Gordon Henriksen14a55692007-12-10 02:14:30 +000039//===----------------------------------------------------------------------===//
40// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43// Out of line virtual method, so the vtable, etc has a home.
44TerminatorInst::~TerminatorInst() {
45}
46
Gabor Greiff6caff662008-05-10 08:32:32 +000047//===----------------------------------------------------------------------===//
48// UnaryInstruction Class
49//===----------------------------------------------------------------------===//
50
Gordon Henriksen14a55692007-12-10 02:14:30 +000051// Out of line virtual method, so the vtable, etc has a home.
52UnaryInstruction::~UnaryInstruction() {
53}
54
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000056// SelectInst Class
57//===----------------------------------------------------------------------===//
58
59/// areInvalidOperands - Return a string if the specified operands are invalid
60/// for a select operation, otherwise return null.
61const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
64
Chris Lattner229907c2011-07-18 04:54:35 +000065 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000066 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000067 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000068 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000069 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Chris Lattner88107952008-12-29 00:12:50 +000070 if (ET == 0)
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000075 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000076 return "select condition must be i1 or <n x i1>";
77 }
78 return 0;
79}
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000083// PHINode Class
84//===----------------------------------------------------------------------===//
85
86PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000089 ReservedSpace(PN.getNumOperands()) {
Jay Foad61ea0e42011-06-23 09:09:15 +000090 std::copy(PN.op_begin(), PN.op_end(), op_begin());
91 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000092 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000093}
94
Gordon Henriksen14a55692007-12-10 02:14:30 +000095PHINode::~PHINode() {
Jay Foadbbb91f22011-01-16 15:30:52 +000096 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097}
98
Jay Foad61ea0e42011-06-23 09:09:15 +000099Use *PHINode::allocHungoffUses(unsigned N) const {
100 // Allocate the array of Uses of the incoming values, followed by a pointer
101 // (with bottom bit set) to the User, followed by the array of pointers to
102 // the incoming basic blocks.
103 size_t size = N * sizeof(Use) + sizeof(Use::UserRef)
104 + N * sizeof(BasicBlock*);
105 Use *Begin = static_cast<Use*>(::operator new(size));
106 Use *End = Begin + N;
107 (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1);
108 return Use::initTags(Begin, End);
109}
110
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000111// removeIncomingValue - Remove an incoming value. This is useful if a
112// predecessor basic block is deleted.
113Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000114 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115
116 // Move everything after this operand down.
117 //
118 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000121 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
122 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000123
124 // Nuke the last value.
Jay Foad61ea0e42011-06-23 09:09:15 +0000125 Op<-1>().set(0);
126 --NumOperands;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000127
128 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000129 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000131 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000132 eraseFromParent();
133 }
134 return Removed;
135}
136
Jay Foade98f29d2011-04-01 08:00:58 +0000137/// growOperands - grow operands - This grows the operand list in response
138/// to a push_back style of operation. This grows the number of ops by 1.5
139/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000140///
Jay Foade98f29d2011-04-01 08:00:58 +0000141void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000142 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000143 unsigned NumOps = e + e / 2;
144 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
145
146 Use *OldOps = op_begin();
147 BasicBlock **OldBlocks = block_begin();
Jay Foade03c05c2011-06-20 14:38:01 +0000148
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 ReservedSpace = NumOps;
Jay Foad61ea0e42011-06-23 09:09:15 +0000150 OperandList = allocHungoffUses(ReservedSpace);
151
152 std::copy(OldOps, OldOps + e, op_begin());
153 std::copy(OldBlocks, OldBlocks + e, block_begin());
154
Jay Foadbbb91f22011-01-16 15:30:52 +0000155 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156}
157
Nate Begemanb3923212005-08-04 23:24:19 +0000158/// hasConstantValue - If the specified PHI node always merges together the same
159/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000160Value *PHINode::hasConstantValue() const {
161 // Exploit the fact that phi nodes always have at least one entry.
162 Value *ConstantValue = getIncomingValue(0);
163 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000164 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
165 if (ConstantValue != this)
166 return 0; // Incoming values not all the same.
167 // The case where the first value is this PHI.
168 ConstantValue = getIncomingValue(i);
169 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000170 if (ConstantValue == this)
171 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000172 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000173}
174
Bill Wendlingfae14752011-08-12 20:24:12 +0000175//===----------------------------------------------------------------------===//
176// LandingPadInst Implementation
177//===----------------------------------------------------------------------===//
178
179LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
180 unsigned NumReservedValues, const Twine &NameStr,
181 Instruction *InsertBefore)
182 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) {
183 init(PersonalityFn, 1 + NumReservedValues, NameStr);
184}
185
186LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
187 unsigned NumReservedValues, const Twine &NameStr,
188 BasicBlock *InsertAtEnd)
189 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) {
190 init(PersonalityFn, 1 + NumReservedValues, NameStr);
191}
192
193LandingPadInst::LandingPadInst(const LandingPadInst &LP)
194 : Instruction(LP.getType(), Instruction::LandingPad,
195 allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()),
196 ReservedSpace(LP.getNumOperands()) {
197 Use *OL = OperandList, *InOL = LP.OperandList;
198 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
199 OL[I] = InOL[I];
200
201 setCleanup(LP.isCleanup());
202}
203
204LandingPadInst::~LandingPadInst() {
205 dropHungoffUses();
206}
207
208LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
209 unsigned NumReservedClauses,
210 const Twine &NameStr,
211 Instruction *InsertBefore) {
212 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
213 InsertBefore);
214}
215
216LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
217 unsigned NumReservedClauses,
218 const Twine &NameStr,
219 BasicBlock *InsertAtEnd) {
220 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
221 InsertAtEnd);
222}
223
224void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
225 const Twine &NameStr) {
226 ReservedSpace = NumReservedValues;
227 NumOperands = 1;
228 OperandList = allocHungoffUses(ReservedSpace);
229 OperandList[0] = PersFn;
230 setName(NameStr);
231 setCleanup(false);
232}
233
234/// growOperands - grow operands - This grows the operand list in response to a
235/// push_back style of operation. This grows the number of ops by 2 times.
236void LandingPadInst::growOperands(unsigned Size) {
237 unsigned e = getNumOperands();
238 if (ReservedSpace >= e + Size) return;
239 ReservedSpace = (e + Size / 2) * 2;
240
241 Use *NewOps = allocHungoffUses(ReservedSpace);
242 Use *OldOps = OperandList;
243 for (unsigned i = 0; i != e; ++i)
244 NewOps[i] = OldOps[i];
245
246 OperandList = NewOps;
247 Use::zap(OldOps, OldOps + e, true);
248}
249
250void LandingPadInst::addClause(Value *Val) {
251 unsigned OpNo = getNumOperands();
252 growOperands(1);
253 assert(OpNo < ReservedSpace && "Growing didn't work!");
254 ++NumOperands;
255 OperandList[OpNo] = Val;
256}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000257
258//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000259// CallInst Implementation
260//===----------------------------------------------------------------------===//
261
Gordon Henriksen14a55692007-12-10 02:14:30 +0000262CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000263}
264
Jay Foad5bd375a2011-07-15 08:37:34 +0000265void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
266 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000267 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268
Jay Foad5bd375a2011-07-15 08:37:34 +0000269#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000270 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000271 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
272
Jay Foad5bd375a2011-07-15 08:37:34 +0000273 assert((Args.size() == FTy->getNumParams() ||
274 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000275 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000276
277 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000278 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000279 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000280 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000281#endif
282
283 std::copy(Args.begin(), Args.end(), op_begin());
284 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285}
286
Jay Foad5bd375a2011-07-15 08:37:34 +0000287void CallInst::init(Value *Func, const Twine &NameStr) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000288 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000289 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000290
Jay Foad5bd375a2011-07-15 08:37:34 +0000291#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000292 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000293 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
294
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000295 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000296#endif
297
298 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299}
300
Daniel Dunbar4975db62009-07-25 04:41:11 +0000301CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 Instruction *InsertBefore)
303 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
304 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000305 Instruction::Call,
306 OperandTraits<CallInst>::op_end(this) - 1,
307 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000308 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309}
310
Daniel Dunbar4975db62009-07-25 04:41:11 +0000311CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 BasicBlock *InsertAtEnd)
313 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
314 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000315 Instruction::Call,
316 OperandTraits<CallInst>::op_end(this) - 1,
317 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000318 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319}
320
Misha Brukmanb1c93172005-04-21 23:48:37 +0000321CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000322 : Instruction(CI.getType(), Instruction::Call,
323 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000324 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000325 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000326 setTailCall(CI.isTailCall());
327 setCallingConv(CI.getCallingConv());
328
Jay Foad5bd375a2011-07-15 08:37:34 +0000329 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000330 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331}
332
Devang Patel4c758ea2008-09-25 21:00:45 +0000333void CallInst::addAttribute(unsigned i, Attributes attr) {
334 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000335 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000336 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000337}
338
Devang Patel4c758ea2008-09-25 21:00:45 +0000339void CallInst::removeAttribute(unsigned i, Attributes attr) {
340 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000341 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000342 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000343}
344
Bill Wendling8baa61d2012-10-03 17:54:26 +0000345bool CallInst::paramHasSExtAttr(unsigned i) const {
346 if (AttributeList.getParamAttributes(i).hasSExtAttr())
347 return true;
348 if (const Function *F = getCalledFunction())
349 return F->getParamAttributes(i).hasSExtAttr();
350 return false;
351}
352
353bool CallInst::paramHasZExtAttr(unsigned i) const {
354 if (AttributeList.getParamAttributes(i).hasZExtAttr())
355 return true;
356 if (const Function *F = getCalledFunction())
357 return F->getParamAttributes(i).hasZExtAttr();
358 return false;
359}
360
361bool CallInst::paramHasInRegAttr(unsigned i) const {
362 if (AttributeList.getParamAttributes(i).hasInRegAttr())
363 return true;
364 if (const Function *F = getCalledFunction())
365 return F->getParamAttributes(i).hasInRegAttr();
366 return false;
367}
368
369bool CallInst::paramHasStructRetAttr(unsigned i) const {
370 if (AttributeList.getParamAttributes(i).hasStructRetAttr())
371 return true;
372 if (const Function *F = getCalledFunction())
373 return F->getParamAttributes(i).hasStructRetAttr();
374 return false;
375}
376
377bool CallInst::paramHasNestAttr(unsigned i) const {
378 if (AttributeList.getParamAttributes(i).hasNestAttr())
379 return true;
380 if (const Function *F = getCalledFunction())
381 return F->getParamAttributes(i).hasNestAttr();
382 return false;
383}
384
385bool CallInst::paramHasByValAttr(unsigned i) const {
386 if (AttributeList.getParamAttributes(i).hasByValAttr())
387 return true;
388 if (const Function *F = getCalledFunction())
389 return F->getParamAttributes(i).hasByValAttr();
390 return false;
391}
392
Bill Wendlingd7773982012-10-04 06:52:09 +0000393bool CallInst::paramHasNoAliasAttr(unsigned i) const {
394 if (AttributeList.getParamAttributes(i).hasNoAliasAttr())
395 return true;
396 if (const Function *F = getCalledFunction())
397 return F->getParamAttributes(i).hasNoAliasAttr();
398 return false;
399}
400
Devang Patelba3fa6c2008-09-23 23:03:40 +0000401bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000402 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000403 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000404 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000405 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000406 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000407}
408
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000409/// IsConstantOne - Return true only if val is constant int 1
410static bool IsConstantOne(Value *val) {
411 assert(val && "IsConstantOne does not work with NULL val");
412 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
413}
414
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000415static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000416 BasicBlock *InsertAtEnd, Type *IntPtrTy,
417 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000418 Value *ArraySize, Function *MallocF,
419 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000420 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000421 "createMalloc needs either InsertBefore or InsertAtEnd");
422
423 // malloc(type) becomes:
424 // bitcast (i8* malloc(typeSize)) to type*
425 // malloc(type, arraySize) becomes:
426 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000427 if (!ArraySize)
428 ArraySize = ConstantInt::get(IntPtrTy, 1);
429 else if (ArraySize->getType() != IntPtrTy) {
430 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000431 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
432 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000433 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000434 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
435 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000436 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000437
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000438 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000439 if (IsConstantOne(AllocSize)) {
440 AllocSize = ArraySize; // Operand * 1 = Operand
441 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
442 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
443 false /*ZExt*/);
444 // Malloc arg is constant product of type size and array size
445 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
446 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000447 // Multiply type size by the array size...
448 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000449 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
450 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000451 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000452 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
453 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000454 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000455 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000456
Victor Hernandez788eaab2009-09-18 19:20:02 +0000457 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000458 // Create the call to Malloc.
459 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
460 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000461 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000462 Value *MallocFunc = MallocF;
463 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000464 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000465 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000466 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000467 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000468 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000469 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000470 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000471 Result = MCall;
472 if (Result->getType() != AllocPtrType)
473 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000474 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000475 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000476 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000477 Result = MCall;
478 if (Result->getType() != AllocPtrType) {
479 InsertAtEnd->getInstList().push_back(MCall);
480 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000481 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000482 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000483 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000484 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000485 if (Function *F = dyn_cast<Function>(MallocFunc)) {
486 MCall->setCallingConv(F->getCallingConv());
487 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
488 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000489 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000490
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000491 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000492}
493
494/// CreateMalloc - Generate the IR for a call to malloc:
495/// 1. Compute the malloc call's argument as the specified type's size,
496/// possibly multiplied by the array size if the array size is not
497/// constant 1.
498/// 2. Call malloc with that argument.
499/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000500Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000501 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000502 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000503 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000504 const Twine &Name) {
505 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000506 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000507}
508
509/// CreateMalloc - Generate the IR for a call to malloc:
510/// 1. Compute the malloc call's argument as the specified type's size,
511/// possibly multiplied by the array size if the array size is not
512/// constant 1.
513/// 2. Call malloc with that argument.
514/// 3. Bitcast the result of the malloc call to the specified type.
515/// Note: This function does not add the bitcast to the basic block, that is the
516/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000517Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000518 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000519 Value *AllocSize, Value *ArraySize,
520 Function *MallocF, const Twine &Name) {
521 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000522 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000523}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000524
Victor Hernandeze2971492009-10-24 04:23:03 +0000525static Instruction* createFree(Value* Source, Instruction *InsertBefore,
526 BasicBlock *InsertAtEnd) {
527 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
528 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000529 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000530 "Can not free something of nonpointer type!");
531
532 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
533 Module* M = BB->getParent()->getParent();
534
Chris Lattner229907c2011-07-18 04:54:35 +0000535 Type *VoidTy = Type::getVoidTy(M->getContext());
536 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000537 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000538 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000539 CallInst* Result = NULL;
540 Value *PtrCast = Source;
541 if (InsertBefore) {
542 if (Source->getType() != IntPtrTy)
543 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
544 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
545 } else {
546 if (Source->getType() != IntPtrTy)
547 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
548 Result = CallInst::Create(FreeFunc, PtrCast, "");
549 }
550 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000551 if (Function *F = dyn_cast<Function>(FreeFunc))
552 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000553
554 return Result;
555}
556
557/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000558Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
559 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000560}
561
562/// CreateFree - Generate the IR for a call to the builtin free function.
563/// Note: This function does not add the call to the basic block, that is the
564/// responsibility of the caller.
565Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
566 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
567 assert(FreeCall && "CreateFree did not create a CallInst");
568 return FreeCall;
569}
570
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000571//===----------------------------------------------------------------------===//
572// InvokeInst Implementation
573//===----------------------------------------------------------------------===//
574
575void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000576 ArrayRef<Value *> Args, const Twine &NameStr) {
577 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000578 Op<-3>() = Fn;
579 Op<-2>() = IfNormal;
580 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000581
582#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000583 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000584 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000585
Jay Foad5bd375a2011-07-15 08:37:34 +0000586 assert(((Args.size() == FTy->getNumParams()) ||
587 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000588 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000589
Jay Foad5bd375a2011-07-15 08:37:34 +0000590 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000591 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000592 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000593 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000594#endif
595
596 std::copy(Args.begin(), Args.end(), op_begin());
597 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000598}
599
Misha Brukmanb1c93172005-04-21 23:48:37 +0000600InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000601 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000602 OperandTraits<InvokeInst>::op_end(this)
603 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000604 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000605 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000606 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000607 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000608 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609}
610
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000611BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
612 return getSuccessor(idx);
613}
614unsigned InvokeInst::getNumSuccessorsV() const {
615 return getNumSuccessors();
616}
617void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
618 return setSuccessor(idx, B);
619}
620
Bill Wendling8baa61d2012-10-03 17:54:26 +0000621bool InvokeInst::paramHasSExtAttr(unsigned i) const {
622 if (AttributeList.getParamAttributes(i).hasSExtAttr())
623 return true;
624 if (const Function *F = getCalledFunction())
625 return F->getParamAttributes(i).hasSExtAttr();
626 return false;
627}
628
629bool InvokeInst::paramHasZExtAttr(unsigned i) const {
630 if (AttributeList.getParamAttributes(i).hasZExtAttr())
631 return true;
632 if (const Function *F = getCalledFunction())
633 return F->getParamAttributes(i).hasZExtAttr();
634 return false;
635}
636
637bool InvokeInst::paramHasInRegAttr(unsigned i) const {
638 if (AttributeList.getParamAttributes(i).hasInRegAttr())
639 return true;
640 if (const Function *F = getCalledFunction())
641 return F->getParamAttributes(i).hasInRegAttr();
642 return false;
643}
644
645bool InvokeInst::paramHasStructRetAttr(unsigned i) const {
646 if (AttributeList.getParamAttributes(i).hasStructRetAttr())
647 return true;
648 if (const Function *F = getCalledFunction())
649 return F->getParamAttributes(i).hasStructRetAttr();
650 return false;
651}
652
653bool InvokeInst::paramHasNestAttr(unsigned i) const {
654 if (AttributeList.getParamAttributes(i).hasNestAttr())
655 return true;
656 if (const Function *F = getCalledFunction())
657 return F->getParamAttributes(i).hasNestAttr();
658 return false;
659}
660
661bool InvokeInst::paramHasByValAttr(unsigned i) const {
662 if (AttributeList.getParamAttributes(i).hasByValAttr())
663 return true;
664 if (const Function *F = getCalledFunction())
665 return F->getParamAttributes(i).hasByValAttr();
666 return false;
667}
668
Bill Wendlingd7773982012-10-04 06:52:09 +0000669bool InvokeInst::paramHasNoAliasAttr(unsigned i) const {
670 if (AttributeList.getParamAttributes(i).hasNoAliasAttr())
671 return true;
672 if (const Function *F = getCalledFunction())
673 return F->getParamAttributes(i).hasNoAliasAttr();
674 return false;
675}
676
Devang Patelba3fa6c2008-09-23 23:03:40 +0000677bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000678 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000679 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000680 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000681 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000682 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000683}
684
Devang Patel4c758ea2008-09-25 21:00:45 +0000685void InvokeInst::addAttribute(unsigned i, Attributes attr) {
686 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000687 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000688 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000689}
690
Devang Patel4c758ea2008-09-25 21:00:45 +0000691void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
692 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000693 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000694 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000695}
696
Bill Wendlingfae14752011-08-12 20:24:12 +0000697LandingPadInst *InvokeInst::getLandingPadInst() const {
698 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
699}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000700
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000701//===----------------------------------------------------------------------===//
702// ReturnInst Implementation
703//===----------------------------------------------------------------------===//
704
Chris Lattner2195fc42007-02-24 00:55:48 +0000705ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000706 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000707 OperandTraits<ReturnInst>::op_end(this) -
708 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000709 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000710 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000711 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000712 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000713}
714
Owen Anderson55f1c092009-08-13 21:58:54 +0000715ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
716 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000717 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
718 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000719 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000720 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000721}
Owen Anderson55f1c092009-08-13 21:58:54 +0000722ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
723 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000724 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
725 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000726 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000727 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000728}
Owen Anderson55f1c092009-08-13 21:58:54 +0000729ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
730 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000731 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000732}
733
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000734unsigned ReturnInst::getNumSuccessorsV() const {
735 return getNumSuccessors();
736}
737
Devang Patelae682fb2008-02-26 17:56:20 +0000738/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
739/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000740void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000741 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000742}
743
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000745 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000746}
747
Devang Patel59643e52008-02-23 00:35:18 +0000748ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000749}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000750
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000751//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000752// ResumeInst Implementation
753//===----------------------------------------------------------------------===//
754
755ResumeInst::ResumeInst(const ResumeInst &RI)
756 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
757 OperandTraits<ResumeInst>::op_begin(this), 1) {
758 Op<0>() = RI.Op<0>();
759}
760
761ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
762 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
763 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
764 Op<0>() = Exn;
765}
766
767ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
768 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
769 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
770 Op<0>() = Exn;
771}
772
773unsigned ResumeInst::getNumSuccessorsV() const {
774 return getNumSuccessors();
775}
776
777void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
778 llvm_unreachable("ResumeInst has no successors!");
779}
780
781BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
782 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000783}
784
785//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000786// UnreachableInst Implementation
787//===----------------------------------------------------------------------===//
788
Owen Anderson55f1c092009-08-13 21:58:54 +0000789UnreachableInst::UnreachableInst(LLVMContext &Context,
790 Instruction *InsertBefore)
791 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
792 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000793}
Owen Anderson55f1c092009-08-13 21:58:54 +0000794UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
795 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
796 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000797}
798
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000799unsigned UnreachableInst::getNumSuccessorsV() const {
800 return getNumSuccessors();
801}
802
803void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000804 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000805}
806
807BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000808 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000809}
810
811//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812// BranchInst Implementation
813//===----------------------------------------------------------------------===//
814
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000815void BranchInst::AssertOK() {
816 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000817 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000818 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000819}
820
Chris Lattner2195fc42007-02-24 00:55:48 +0000821BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000822 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000823 OperandTraits<BranchInst>::op_end(this) - 1,
824 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000825 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000826 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000827}
828BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
829 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000830 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000831 OperandTraits<BranchInst>::op_end(this) - 3,
832 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000833 Op<-1>() = IfTrue;
834 Op<-2>() = IfFalse;
835 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000836#ifndef NDEBUG
837 AssertOK();
838#endif
839}
840
841BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000842 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000843 OperandTraits<BranchInst>::op_end(this) - 1,
844 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000845 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000846 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000847}
848
849BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
850 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000851 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000852 OperandTraits<BranchInst>::op_end(this) - 3,
853 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000854 Op<-1>() = IfTrue;
855 Op<-2>() = IfFalse;
856 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000857#ifndef NDEBUG
858 AssertOK();
859#endif
860}
861
862
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000863BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000864 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000865 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
866 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000867 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000868 if (BI.getNumOperands() != 1) {
869 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000870 Op<-3>() = BI.Op<-3>();
871 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000872 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000873 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000874}
875
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000876void BranchInst::swapSuccessors() {
877 assert(isConditional() &&
878 "Cannot swap successors of an unconditional branch");
879 Op<-1>().swap(Op<-2>());
880
881 // Update profile metadata if present and it matches our structural
882 // expectations.
883 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
884 if (!ProfileData || ProfileData->getNumOperands() != 3)
885 return;
886
887 // The first operand is the name. Fetch them backwards and build a new one.
888 Value *Ops[] = {
889 ProfileData->getOperand(0),
890 ProfileData->getOperand(2),
891 ProfileData->getOperand(1)
892 };
893 setMetadata(LLVMContext::MD_prof,
894 MDNode::get(ProfileData->getContext(), Ops));
895}
896
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000897BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
898 return getSuccessor(idx);
899}
900unsigned BranchInst::getNumSuccessorsV() const {
901 return getNumSuccessors();
902}
903void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
904 setSuccessor(idx, B);
905}
906
907
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000908//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000909// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000910//===----------------------------------------------------------------------===//
911
Owen Andersonb6b25302009-07-14 23:09:55 +0000912static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000913 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000914 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000915 else {
916 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000917 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000918 assert(Amt->getType()->isIntegerTy() &&
919 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000920 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000921 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922}
923
Chris Lattner229907c2011-07-18 04:54:35 +0000924AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000925 const Twine &Name, Instruction *InsertBefore)
926 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
927 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
928 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000929 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000930 setName(Name);
931}
932
Chris Lattner229907c2011-07-18 04:54:35 +0000933AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000934 const Twine &Name, BasicBlock *InsertAtEnd)
935 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
936 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
937 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000938 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000939 setName(Name);
940}
941
Chris Lattner229907c2011-07-18 04:54:35 +0000942AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000943 Instruction *InsertBefore)
944 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
945 getAISize(Ty->getContext(), 0), InsertBefore) {
946 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000947 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000948 setName(Name);
949}
950
Chris Lattner229907c2011-07-18 04:54:35 +0000951AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000952 BasicBlock *InsertAtEnd)
953 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
954 getAISize(Ty->getContext(), 0), InsertAtEnd) {
955 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000956 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000957 setName(Name);
958}
959
Chris Lattner229907c2011-07-18 04:54:35 +0000960AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000961 const Twine &Name, Instruction *InsertBefore)
962 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000963 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000964 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000965 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000966 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000967}
968
Chris Lattner229907c2011-07-18 04:54:35 +0000969AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000970 const Twine &Name, BasicBlock *InsertAtEnd)
971 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000972 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000973 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000974 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000975 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000976}
977
Gordon Henriksen14a55692007-12-10 02:14:30 +0000978// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000979AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000980}
981
Victor Hernandez8acf2952009-10-23 21:09:37 +0000982void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000983 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000984 assert(Align <= MaximumAlignment &&
985 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000986 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000987 assert(getAlignment() == Align && "Alignment representation error!");
988}
989
Victor Hernandez8acf2952009-10-23 21:09:37 +0000990bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000991 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000992 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000993 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000994}
995
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000996Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000997 return getType()->getElementType();
998}
999
Chris Lattner8b291e62008-11-26 02:54:17 +00001000/// isStaticAlloca - Return true if this alloca is in the entry block of the
1001/// function and is a constant size. If so, the code generator will fold it
1002/// into the prolog/epilog code, so it is basically free.
1003bool AllocaInst::isStaticAlloca() const {
1004 // Must be constant size.
1005 if (!isa<ConstantInt>(getArraySize())) return false;
1006
1007 // Must be in the entry block.
1008 const BasicBlock *Parent = getParent();
1009 return Parent == &Parent->getParent()->front();
1010}
1011
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001012//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001013// LoadInst Implementation
1014//===----------------------------------------------------------------------===//
1015
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001016void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001017 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001018 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001019 assert(!(isAtomic() && getAlignment() == 0) &&
1020 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001021}
1022
Daniel Dunbar4975db62009-07-25 04:41:11 +00001023LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001024 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001025 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001026 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001027 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001028 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001029 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001030 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001031}
1032
Daniel Dunbar4975db62009-07-25 04:41:11 +00001033LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001034 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001035 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001036 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001037 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001038 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001039 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001040 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001041}
1042
Daniel Dunbar4975db62009-07-25 04:41:11 +00001043LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001044 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001045 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001046 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001047 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001048 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001049 setAtomic(NotAtomic);
1050 AssertOK();
1051 setName(Name);
1052}
1053
1054LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1055 BasicBlock *InsertAE)
1056 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1057 Load, Ptr, InsertAE) {
1058 setVolatile(isVolatile);
1059 setAlignment(0);
1060 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001061 AssertOK();
1062 setName(Name);
1063}
1064
Daniel Dunbar4975db62009-07-25 04:41:11 +00001065LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001066 unsigned Align, Instruction *InsertBef)
1067 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1068 Load, Ptr, InsertBef) {
1069 setVolatile(isVolatile);
1070 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001071 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001072 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001073 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001074}
1075
Daniel Dunbar4975db62009-07-25 04:41:11 +00001076LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001077 unsigned Align, BasicBlock *InsertAE)
1078 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1079 Load, Ptr, InsertAE) {
1080 setVolatile(isVolatile);
1081 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001082 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +00001083 AssertOK();
1084 setName(Name);
1085}
1086
Eli Friedman59b66882011-08-09 23:02:53 +00001087LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1088 unsigned Align, AtomicOrdering Order,
1089 SynchronizationScope SynchScope,
1090 Instruction *InsertBef)
1091 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1092 Load, Ptr, InsertBef) {
1093 setVolatile(isVolatile);
1094 setAlignment(Align);
1095 setAtomic(Order, SynchScope);
1096 AssertOK();
1097 setName(Name);
1098}
1099
1100LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1101 unsigned Align, AtomicOrdering Order,
1102 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001103 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001104 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001105 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001106 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001107 setAlignment(Align);
1108 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001109 AssertOK();
1110 setName(Name);
1111}
1112
Daniel Dunbar27096822009-08-11 18:11:15 +00001113LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1114 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1115 Load, Ptr, InsertBef) {
1116 setVolatile(false);
1117 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001118 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001119 AssertOK();
1120 if (Name && Name[0]) setName(Name);
1121}
1122
1123LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1124 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1125 Load, Ptr, InsertAE) {
1126 setVolatile(false);
1127 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001128 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001129 AssertOK();
1130 if (Name && Name[0]) setName(Name);
1131}
1132
1133LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1134 Instruction *InsertBef)
1135: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1136 Load, Ptr, InsertBef) {
1137 setVolatile(isVolatile);
1138 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001139 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001140 AssertOK();
1141 if (Name && Name[0]) setName(Name);
1142}
1143
1144LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1145 BasicBlock *InsertAE)
1146 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1147 Load, Ptr, InsertAE) {
1148 setVolatile(isVolatile);
1149 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001150 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001151 AssertOK();
1152 if (Name && Name[0]) setName(Name);
1153}
1154
Christopher Lamb84485702007-04-22 19:24:39 +00001155void LoadInst::setAlignment(unsigned Align) {
1156 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001157 assert(Align <= MaximumAlignment &&
1158 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001159 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001160 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001161 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001162}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001163
1164//===----------------------------------------------------------------------===//
1165// StoreInst Implementation
1166//===----------------------------------------------------------------------===//
1167
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001168void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001169 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001170 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001171 "Ptr must have pointer type!");
1172 assert(getOperand(0)->getType() ==
1173 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001174 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001175 assert(!(isAtomic() && getAlignment() == 0) &&
1176 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001177}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001178
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001179
1180StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001181 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001182 OperandTraits<StoreInst>::op_begin(this),
1183 OperandTraits<StoreInst>::operands(this),
1184 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001185 Op<0>() = val;
1186 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001187 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001188 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001189 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001190 AssertOK();
1191}
1192
1193StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001194 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001195 OperandTraits<StoreInst>::op_begin(this),
1196 OperandTraits<StoreInst>::operands(this),
1197 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001198 Op<0>() = val;
1199 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001200 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001201 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001202 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001203 AssertOK();
1204}
1205
Misha Brukmanb1c93172005-04-21 23:48:37 +00001206StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001207 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001208 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001209 OperandTraits<StoreInst>::op_begin(this),
1210 OperandTraits<StoreInst>::operands(this),
1211 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001212 Op<0>() = val;
1213 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001214 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001215 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001216 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001217 AssertOK();
1218}
1219
1220StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1221 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001222 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001223 OperandTraits<StoreInst>::op_begin(this),
1224 OperandTraits<StoreInst>::operands(this),
1225 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001226 Op<0>() = val;
1227 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001228 setVolatile(isVolatile);
1229 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001230 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001231 AssertOK();
1232}
1233
Misha Brukmanb1c93172005-04-21 23:48:37 +00001234StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001235 unsigned Align, AtomicOrdering Order,
1236 SynchronizationScope SynchScope,
1237 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001238 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001239 OperandTraits<StoreInst>::op_begin(this),
1240 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001241 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001242 Op<0>() = val;
1243 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001244 setVolatile(isVolatile);
1245 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001246 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001247 AssertOK();
1248}
1249
1250StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001251 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001252 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001253 OperandTraits<StoreInst>::op_begin(this),
1254 OperandTraits<StoreInst>::operands(this),
1255 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001256 Op<0>() = val;
1257 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001258 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001259 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001260 setAtomic(NotAtomic);
1261 AssertOK();
1262}
1263
1264StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1265 unsigned Align, BasicBlock *InsertAtEnd)
1266 : Instruction(Type::getVoidTy(val->getContext()), Store,
1267 OperandTraits<StoreInst>::op_begin(this),
1268 OperandTraits<StoreInst>::operands(this),
1269 InsertAtEnd) {
1270 Op<0>() = val;
1271 Op<1>() = addr;
1272 setVolatile(isVolatile);
1273 setAlignment(Align);
1274 setAtomic(NotAtomic);
1275 AssertOK();
1276}
1277
1278StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1279 unsigned Align, AtomicOrdering Order,
1280 SynchronizationScope SynchScope,
1281 BasicBlock *InsertAtEnd)
1282 : Instruction(Type::getVoidTy(val->getContext()), Store,
1283 OperandTraits<StoreInst>::op_begin(this),
1284 OperandTraits<StoreInst>::operands(this),
1285 InsertAtEnd) {
1286 Op<0>() = val;
1287 Op<1>() = addr;
1288 setVolatile(isVolatile);
1289 setAlignment(Align);
1290 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001291 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001292}
1293
Christopher Lamb84485702007-04-22 19:24:39 +00001294void StoreInst::setAlignment(unsigned Align) {
1295 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001296 assert(Align <= MaximumAlignment &&
1297 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001298 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001299 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001300 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001301}
1302
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001303//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001304// AtomicCmpXchgInst Implementation
1305//===----------------------------------------------------------------------===//
1306
1307void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1308 AtomicOrdering Ordering,
1309 SynchronizationScope SynchScope) {
1310 Op<0>() = Ptr;
1311 Op<1>() = Cmp;
1312 Op<2>() = NewVal;
1313 setOrdering(Ordering);
1314 setSynchScope(SynchScope);
1315
1316 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1317 "All operands must be non-null!");
1318 assert(getOperand(0)->getType()->isPointerTy() &&
1319 "Ptr must have pointer type!");
1320 assert(getOperand(1)->getType() ==
1321 cast<PointerType>(getOperand(0)->getType())->getElementType()
1322 && "Ptr must be a pointer to Cmp type!");
1323 assert(getOperand(2)->getType() ==
1324 cast<PointerType>(getOperand(0)->getType())->getElementType()
1325 && "Ptr must be a pointer to NewVal type!");
1326 assert(Ordering != NotAtomic &&
1327 "AtomicCmpXchg instructions must be atomic!");
1328}
1329
1330AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1331 AtomicOrdering Ordering,
1332 SynchronizationScope SynchScope,
1333 Instruction *InsertBefore)
1334 : Instruction(Cmp->getType(), AtomicCmpXchg,
1335 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1336 OperandTraits<AtomicCmpXchgInst>::operands(this),
1337 InsertBefore) {
1338 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1339}
1340
1341AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1342 AtomicOrdering Ordering,
1343 SynchronizationScope SynchScope,
1344 BasicBlock *InsertAtEnd)
1345 : Instruction(Cmp->getType(), AtomicCmpXchg,
1346 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1347 OperandTraits<AtomicCmpXchgInst>::operands(this),
1348 InsertAtEnd) {
1349 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1350}
1351
1352//===----------------------------------------------------------------------===//
1353// AtomicRMWInst Implementation
1354//===----------------------------------------------------------------------===//
1355
1356void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1357 AtomicOrdering Ordering,
1358 SynchronizationScope SynchScope) {
1359 Op<0>() = Ptr;
1360 Op<1>() = Val;
1361 setOperation(Operation);
1362 setOrdering(Ordering);
1363 setSynchScope(SynchScope);
1364
1365 assert(getOperand(0) && getOperand(1) &&
1366 "All operands must be non-null!");
1367 assert(getOperand(0)->getType()->isPointerTy() &&
1368 "Ptr must have pointer type!");
1369 assert(getOperand(1)->getType() ==
1370 cast<PointerType>(getOperand(0)->getType())->getElementType()
1371 && "Ptr must be a pointer to Val type!");
1372 assert(Ordering != NotAtomic &&
1373 "AtomicRMW instructions must be atomic!");
1374}
1375
1376AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1377 AtomicOrdering Ordering,
1378 SynchronizationScope SynchScope,
1379 Instruction *InsertBefore)
1380 : Instruction(Val->getType(), AtomicRMW,
1381 OperandTraits<AtomicRMWInst>::op_begin(this),
1382 OperandTraits<AtomicRMWInst>::operands(this),
1383 InsertBefore) {
1384 Init(Operation, Ptr, Val, Ordering, SynchScope);
1385}
1386
1387AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1388 AtomicOrdering Ordering,
1389 SynchronizationScope SynchScope,
1390 BasicBlock *InsertAtEnd)
1391 : Instruction(Val->getType(), AtomicRMW,
1392 OperandTraits<AtomicRMWInst>::op_begin(this),
1393 OperandTraits<AtomicRMWInst>::operands(this),
1394 InsertAtEnd) {
1395 Init(Operation, Ptr, Val, Ordering, SynchScope);
1396}
1397
1398//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001399// FenceInst Implementation
1400//===----------------------------------------------------------------------===//
1401
1402FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1403 SynchronizationScope SynchScope,
1404 Instruction *InsertBefore)
1405 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1406 setOrdering(Ordering);
1407 setSynchScope(SynchScope);
1408}
1409
1410FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1411 SynchronizationScope SynchScope,
1412 BasicBlock *InsertAtEnd)
1413 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1414 setOrdering(Ordering);
1415 setSynchScope(SynchScope);
1416}
1417
1418//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001419// GetElementPtrInst Implementation
1420//===----------------------------------------------------------------------===//
1421
Jay Foadd1b78492011-07-25 09:48:08 +00001422void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001423 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001424 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1425 OperandList[0] = Ptr;
1426 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001427 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001428}
1429
Gabor Greiff6caff662008-05-10 08:32:32 +00001430GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001431 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001432 OperandTraits<GetElementPtrInst>::op_end(this)
1433 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001434 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001435 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001436 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001437}
1438
Chris Lattner6090a422009-03-09 04:46:40 +00001439/// getIndexedType - Returns the type of the element that would be accessed with
1440/// a gep instruction with the specified parameters.
1441///
1442/// The Idxs pointer should point to a continuous piece of memory containing the
1443/// indices, either as Value* or uint64_t.
1444///
1445/// A null type is returned if the indices are invalid for the specified
1446/// pointer type.
1447///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001448template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001449static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001450 if (Ptr->isVectorTy()) {
1451 assert(IdxList.size() == 1 &&
1452 "GEP with vector pointers must have a single index");
1453 PointerType *PTy = dyn_cast<PointerType>(
1454 cast<VectorType>(Ptr)->getElementType());
1455 assert(PTy && "Gep with invalid vector pointer found");
1456 return PTy->getElementType();
1457 }
1458
Chris Lattner229907c2011-07-18 04:54:35 +00001459 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001460 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001461 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001462
Chris Lattner6090a422009-03-09 04:46:40 +00001463 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001464 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001465 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001466
Chris Lattner6090a422009-03-09 04:46:40 +00001467 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001468 // it cannot be 'stepped over'.
1469 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001470 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001471
Dan Gohman1ecaf452008-05-31 00:58:22 +00001472 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001473 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001474 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001475 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001476 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001477 if (!CT->indexValid(Index)) return 0;
1478 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001479 }
Jay Foadd1b78492011-07-25 09:48:08 +00001480 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001481}
1482
Jay Foadd1b78492011-07-25 09:48:08 +00001483Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1484 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001485}
1486
Chris Lattner229907c2011-07-18 04:54:35 +00001487Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001488 ArrayRef<Constant *> IdxList) {
1489 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001490}
1491
Jay Foadd1b78492011-07-25 09:48:08 +00001492Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1493 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001494}
1495
Nadav Rotem3924cb02011-12-05 06:29:09 +00001496unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) {
1497 Type *Ty = Ptr->getType();
1498
1499 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1500 Ty = VTy->getElementType();
1501
1502 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
1503 return PTy->getAddressSpace();
1504
Craig Topperc514b542012-02-05 22:14:15 +00001505 llvm_unreachable("Invalid GEP pointer type");
Nadav Rotem3924cb02011-12-05 06:29:09 +00001506}
1507
Chris Lattner45f15572007-04-14 00:12:57 +00001508/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1509/// zeros. If so, the result pointer and the first operand have the same
1510/// value, just potentially different types.
1511bool GetElementPtrInst::hasAllZeroIndices() const {
1512 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1513 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1514 if (!CI->isZero()) return false;
1515 } else {
1516 return false;
1517 }
1518 }
1519 return true;
1520}
1521
Chris Lattner27058292007-04-27 20:35:56 +00001522/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1523/// constant integers. If so, the result pointer and the first operand have
1524/// a constant offset between them.
1525bool GetElementPtrInst::hasAllConstantIndices() const {
1526 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1527 if (!isa<ConstantInt>(getOperand(i)))
1528 return false;
1529 }
1530 return true;
1531}
1532
Dan Gohman1b849082009-09-07 23:54:19 +00001533void GetElementPtrInst::setIsInBounds(bool B) {
1534 cast<GEPOperator>(this)->setIsInBounds(B);
1535}
Chris Lattner45f15572007-04-14 00:12:57 +00001536
Nick Lewycky28a5f252009-09-27 21:33:04 +00001537bool GetElementPtrInst::isInBounds() const {
1538 return cast<GEPOperator>(this)->isInBounds();
1539}
1540
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001541//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001542// ExtractElementInst Implementation
1543//===----------------------------------------------------------------------===//
1544
1545ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001546 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001547 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001548 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001549 ExtractElement,
1550 OperandTraits<ExtractElementInst>::op_begin(this),
1551 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001552 assert(isValidOperands(Val, Index) &&
1553 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001554 Op<0>() = Val;
1555 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001556 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001557}
1558
1559ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001560 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001561 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001562 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001563 ExtractElement,
1564 OperandTraits<ExtractElementInst>::op_begin(this),
1565 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001566 assert(isValidOperands(Val, Index) &&
1567 "Invalid extractelement instruction operands!");
1568
Gabor Greif2d3024d2008-05-26 21:33:52 +00001569 Op<0>() = Val;
1570 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001571 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001572}
1573
Chris Lattner65511ff2006-10-05 06:24:58 +00001574
Chris Lattner54865b32006-04-08 04:05:48 +00001575bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001576 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001577 return false;
1578 return true;
1579}
1580
1581
Robert Bocchino23004482006-01-10 19:05:34 +00001582//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001583// InsertElementInst Implementation
1584//===----------------------------------------------------------------------===//
1585
Chris Lattner54865b32006-04-08 04:05:48 +00001586InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001587 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001588 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001589 : Instruction(Vec->getType(), InsertElement,
1590 OperandTraits<InsertElementInst>::op_begin(this),
1591 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001592 assert(isValidOperands(Vec, Elt, Index) &&
1593 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001594 Op<0>() = Vec;
1595 Op<1>() = Elt;
1596 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001597 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001598}
1599
Chris Lattner54865b32006-04-08 04:05:48 +00001600InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001601 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001602 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001603 : Instruction(Vec->getType(), InsertElement,
1604 OperandTraits<InsertElementInst>::op_begin(this),
1605 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001606 assert(isValidOperands(Vec, Elt, Index) &&
1607 "Invalid insertelement instruction operands!");
1608
Gabor Greif2d3024d2008-05-26 21:33:52 +00001609 Op<0>() = Vec;
1610 Op<1>() = Elt;
1611 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001612 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001613}
1614
Chris Lattner54865b32006-04-08 04:05:48 +00001615bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1616 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001617 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001618 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001619
Reid Spencerd84d35b2007-02-15 02:26:10 +00001620 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001621 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001622
Duncan Sands9dff9be2010-02-15 16:12:20 +00001623 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001624 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001625 return true;
1626}
1627
1628
Robert Bocchinoca27f032006-01-17 20:07:22 +00001629//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001630// ShuffleVectorInst Implementation
1631//===----------------------------------------------------------------------===//
1632
1633ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001634 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001635 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001636: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001637 cast<VectorType>(Mask->getType())->getNumElements()),
1638 ShuffleVector,
1639 OperandTraits<ShuffleVectorInst>::op_begin(this),
1640 OperandTraits<ShuffleVectorInst>::operands(this),
1641 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001642 assert(isValidOperands(V1, V2, Mask) &&
1643 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001644 Op<0>() = V1;
1645 Op<1>() = V2;
1646 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001647 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001648}
1649
1650ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001651 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001652 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001653: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1654 cast<VectorType>(Mask->getType())->getNumElements()),
1655 ShuffleVector,
1656 OperandTraits<ShuffleVectorInst>::op_begin(this),
1657 OperandTraits<ShuffleVectorInst>::operands(this),
1658 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001659 assert(isValidOperands(V1, V2, Mask) &&
1660 "Invalid shuffle vector instruction operands!");
1661
Gabor Greif2d3024d2008-05-26 21:33:52 +00001662 Op<0>() = V1;
1663 Op<1>() = V2;
1664 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001665 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001666}
1667
Mon P Wang25f01062008-11-10 04:46:22 +00001668bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001669 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001670 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001671 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001672 return false;
1673
Chris Lattner1dcb6542012-01-25 23:49:49 +00001674 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001675 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001676 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001677 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001678
1679 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001680 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1681 return true;
1682
Nate Begeman60a31c32010-08-13 00:16:46 +00001683 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001684 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001685 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001686 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1687 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001688 return false;
1689 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1690 return false;
1691 }
1692 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001693 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001694 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001695
1696 if (const ConstantDataSequential *CDS =
1697 dyn_cast<ConstantDataSequential>(Mask)) {
1698 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1699 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1700 if (CDS->getElementAsInteger(i) >= V1Size*2)
1701 return false;
1702 return true;
1703 }
1704
1705 // The bitcode reader can create a place holder for a forward reference
1706 // used as the shuffle mask. When this occurs, the shuffle mask will
1707 // fall into this case and fail. To avoid this error, do this bit of
1708 // ugliness to allow such a mask pass.
1709 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1710 if (CE->getOpcode() == Instruction::UserOp1)
1711 return true;
1712
1713 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001714}
1715
Chris Lattnerf724e342008-03-02 05:28:33 +00001716/// getMaskValue - Return the index from the shuffle mask for the specified
1717/// output result. This is either -1 if the element is undef or a number less
1718/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001719int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1720 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1721 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001722 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001723 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001724 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001725 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001726 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001727}
1728
Chris Lattner1dcb6542012-01-25 23:49:49 +00001729/// getShuffleMask - Return the full mask for this instruction, where each
1730/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001731void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1732 SmallVectorImpl<int> &Result) {
1733 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001734
Chris Lattnercf129702012-01-26 02:51:13 +00001735 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001736 for (unsigned i = 0; i != NumElts; ++i)
1737 Result.push_back(CDS->getElementAsInteger(i));
1738 return;
1739 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001740 for (unsigned i = 0; i != NumElts; ++i) {
1741 Constant *C = Mask->getAggregateElement(i);
1742 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001743 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001744 }
1745}
1746
1747
Dan Gohman12fce772008-05-15 19:50:34 +00001748//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001749// InsertValueInst Class
1750//===----------------------------------------------------------------------===//
1751
Jay Foad57aa6362011-07-13 10:26:04 +00001752void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1753 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001754 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001755
1756 // There's no fundamental reason why we require at least one index
1757 // (other than weirdness with &*IdxBegin being invalid; see
1758 // getelementptr's init routine for example). But there's no
1759 // present need to support it.
1760 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1761
1762 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001763 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001764 Op<0>() = Agg;
1765 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001766
Jay Foad57aa6362011-07-13 10:26:04 +00001767 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001768 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001769}
1770
1771InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001772 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001773 OperandTraits<InsertValueInst>::op_begin(this), 2),
1774 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001775 Op<0>() = IVI.getOperand(0);
1776 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001777 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001778}
1779
1780//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001781// ExtractValueInst Class
1782//===----------------------------------------------------------------------===//
1783
Jay Foad57aa6362011-07-13 10:26:04 +00001784void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001785 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001786
Jay Foad57aa6362011-07-13 10:26:04 +00001787 // There's no fundamental reason why we require at least one index.
1788 // But there's no present need to support it.
1789 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001790
Jay Foad57aa6362011-07-13 10:26:04 +00001791 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001792 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001793}
1794
1795ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001796 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001797 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001798 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001799}
1800
Dan Gohman12fce772008-05-15 19:50:34 +00001801// getIndexedType - Returns the type of the element that would be extracted
1802// with an extractvalue instruction with the specified parameters.
1803//
1804// A null type is returned if the indices are invalid for the specified
1805// pointer type.
1806//
Chris Lattner229907c2011-07-18 04:54:35 +00001807Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001808 ArrayRef<unsigned> Idxs) {
1809 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001810 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001811 // We can't use CompositeType::indexValid(Index) here.
1812 // indexValid() always returns true for arrays because getelementptr allows
1813 // out-of-bounds indices. Since we don't allow those for extractvalue and
1814 // insertvalue we need to check array indexing manually.
1815 // Since the only other types we can index into are struct types it's just
1816 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001817 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001818 if (Index >= AT->getNumElements())
1819 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001820 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001821 if (Index >= ST->getNumElements())
1822 return 0;
1823 } else {
1824 // Not a valid type to index into.
1825 return 0;
1826 }
1827
1828 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001829 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001830 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001831}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001832
1833//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001834// BinaryOperator Class
1835//===----------------------------------------------------------------------===//
1836
Chris Lattner2195fc42007-02-24 00:55:48 +00001837BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001838 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001839 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001840 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001841 OperandTraits<BinaryOperator>::op_begin(this),
1842 OperandTraits<BinaryOperator>::operands(this),
1843 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001844 Op<0>() = S1;
1845 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001846 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001847 setName(Name);
1848}
1849
1850BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001851 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001852 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001853 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001854 OperandTraits<BinaryOperator>::op_begin(this),
1855 OperandTraits<BinaryOperator>::operands(this),
1856 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001857 Op<0>() = S1;
1858 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001859 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001860 setName(Name);
1861}
1862
1863
1864void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001865 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001866 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001867 assert(LHS->getType() == RHS->getType() &&
1868 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001869#ifndef NDEBUG
1870 switch (iType) {
1871 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001872 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001873 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001874 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001875 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001876 "Tried to create an integer operation on a non-integer type!");
1877 break;
1878 case FAdd: case FSub:
1879 case FMul:
1880 assert(getType() == LHS->getType() &&
1881 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001882 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001883 "Tried to create a floating-point operation on a "
1884 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001885 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001886 case UDiv:
1887 case SDiv:
1888 assert(getType() == LHS->getType() &&
1889 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001890 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001891 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001892 "Incorrect operand type (not integer) for S/UDIV");
1893 break;
1894 case FDiv:
1895 assert(getType() == LHS->getType() &&
1896 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001897 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001898 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001899 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001900 case URem:
1901 case SRem:
1902 assert(getType() == LHS->getType() &&
1903 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001904 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001905 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001906 "Incorrect operand type (not integer) for S/UREM");
1907 break;
1908 case FRem:
1909 assert(getType() == LHS->getType() &&
1910 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001911 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001912 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001913 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001914 case Shl:
1915 case LShr:
1916 case AShr:
1917 assert(getType() == LHS->getType() &&
1918 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001919 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001920 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001921 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001922 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001923 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001924 case And: case Or:
1925 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001926 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001927 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001928 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001929 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001930 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001931 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001932 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001933 default:
1934 break;
1935 }
1936#endif
1937}
1938
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001939BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001940 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001941 Instruction *InsertBefore) {
1942 assert(S1->getType() == S2->getType() &&
1943 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001944 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001945}
1946
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001947BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001948 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001949 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001950 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001951 InsertAtEnd->getInstList().push_back(Res);
1952 return Res;
1953}
1954
Dan Gohman5476cfd2009-08-12 16:23:25 +00001955BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001956 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001957 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001958 return new BinaryOperator(Instruction::Sub,
1959 zero, Op,
1960 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001961}
1962
Dan Gohman5476cfd2009-08-12 16:23:25 +00001963BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001964 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001965 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001966 return new BinaryOperator(Instruction::Sub,
1967 zero, Op,
1968 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001969}
1970
Dan Gohman4ab44202009-12-18 02:58:50 +00001971BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1972 Instruction *InsertBefore) {
1973 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1974 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1975}
1976
1977BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1978 BasicBlock *InsertAtEnd) {
1979 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1980 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1981}
1982
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001983BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1984 Instruction *InsertBefore) {
1985 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1986 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1987}
1988
1989BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1990 BasicBlock *InsertAtEnd) {
1991 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1992 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1993}
1994
Dan Gohman5476cfd2009-08-12 16:23:25 +00001995BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001996 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001997 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001998 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001999 Op->getType(), Name, InsertBefore);
2000}
2001
Dan Gohman5476cfd2009-08-12 16:23:25 +00002002BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002003 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002004 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002005 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002006 Op->getType(), Name, InsertAtEnd);
2007}
2008
Dan Gohman5476cfd2009-08-12 16:23:25 +00002009BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002010 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002011 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002012 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002013 Op->getType(), Name, InsertBefore);
2014}
2015
Dan Gohman5476cfd2009-08-12 16:23:25 +00002016BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002017 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002018 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002019 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002020 Op->getType(), Name, InsertAtEnd);
2021}
2022
2023
2024// isConstantAllOnes - Helper function for several functions below
2025static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002026 if (const Constant *C = dyn_cast<Constant>(V))
2027 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002028 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002029}
2030
Owen Andersonbb2501b2009-07-13 22:18:28 +00002031bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002032 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2033 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002034 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2035 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002036 return false;
2037}
2038
Owen Andersonbb2501b2009-07-13 22:18:28 +00002039bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002040 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2041 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002042 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00002043 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00002044 return false;
2045}
2046
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002047bool BinaryOperator::isNot(const Value *V) {
2048 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2049 return (Bop->getOpcode() == Instruction::Xor &&
2050 (isConstantAllOnes(Bop->getOperand(1)) ||
2051 isConstantAllOnes(Bop->getOperand(0))));
2052 return false;
2053}
2054
Chris Lattner2c7d1772005-04-24 07:28:37 +00002055Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002056 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002057}
2058
Chris Lattner2c7d1772005-04-24 07:28:37 +00002059const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2060 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002061}
2062
Dan Gohmana5b96452009-06-04 22:49:04 +00002063Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002064 return cast<BinaryOperator>(BinOp)->getOperand(1);
2065}
2066
2067const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2068 return getFNegArgument(const_cast<Value*>(BinOp));
2069}
2070
Chris Lattner2c7d1772005-04-24 07:28:37 +00002071Value *BinaryOperator::getNotArgument(Value *BinOp) {
2072 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2073 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2074 Value *Op0 = BO->getOperand(0);
2075 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002076 if (isConstantAllOnes(Op0)) return Op1;
2077
2078 assert(isConstantAllOnes(Op1));
2079 return Op0;
2080}
2081
Chris Lattner2c7d1772005-04-24 07:28:37 +00002082const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2083 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002084}
2085
2086
2087// swapOperands - Exchange the two operands to this instruction. This
2088// instruction is safe to use on any binary instruction and does not
2089// modify the semantics of the instruction. If the instruction is
2090// order dependent (SetLT f.e.) the opcode is changed.
2091//
2092bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002093 if (!isCommutative())
2094 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002095 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002096 return false;
2097}
2098
Dan Gohman1b849082009-09-07 23:54:19 +00002099void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2100 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2101}
2102
2103void BinaryOperator::setHasNoSignedWrap(bool b) {
2104 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2105}
2106
2107void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002108 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002109}
2110
Nick Lewycky28a5f252009-09-27 21:33:04 +00002111bool BinaryOperator::hasNoUnsignedWrap() const {
2112 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2113}
2114
2115bool BinaryOperator::hasNoSignedWrap() const {
2116 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2117}
2118
2119bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002120 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002121}
2122
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002123//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002124// FPMathOperator Class
2125//===----------------------------------------------------------------------===//
2126
2127/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2128/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002129/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002130float FPMathOperator::getFPAccuracy() const {
2131 const MDNode *MD =
2132 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2133 if (!MD)
2134 return 0.0;
Duncan Sands9af62982012-04-16 19:39:33 +00002135 ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2136 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002137}
2138
2139
2140//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002141// CastInst Class
2142//===----------------------------------------------------------------------===//
2143
David Blaikie3a15e142011-12-01 08:00:17 +00002144void CastInst::anchor() {}
2145
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002146// Just determine if this cast only deals with integral->integral conversion.
2147bool CastInst::isIntegerCast() const {
2148 switch (getOpcode()) {
2149 default: return false;
2150 case Instruction::ZExt:
2151 case Instruction::SExt:
2152 case Instruction::Trunc:
2153 return true;
2154 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002155 return getOperand(0)->getType()->isIntegerTy() &&
2156 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002157 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002158}
2159
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002160bool CastInst::isLosslessCast() const {
2161 // Only BitCast can be lossless, exit fast if we're not BitCast
2162 if (getOpcode() != Instruction::BitCast)
2163 return false;
2164
2165 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002166 Type* SrcTy = getOperand(0)->getType();
2167 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002168 if (SrcTy == DstTy)
2169 return true;
2170
Reid Spencer8d9336d2006-12-31 05:26:44 +00002171 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002172 if (SrcTy->isPointerTy())
2173 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002174 return false; // Other types have no identity values
2175}
2176
2177/// This function determines if the CastInst does not require any bits to be
2178/// changed in order to effect the cast. Essentially, it identifies cases where
2179/// no code gen is necessary for the cast, hence the name no-op cast. For
2180/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002181/// # bitcast i32* %x to i8*
2182/// # bitcast <2 x i32> %x to <4 x i16>
2183/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002184/// @brief Determine if the described cast is a no-op.
2185bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002186 Type *SrcTy,
2187 Type *DestTy,
2188 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002189 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002190 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002191 case Instruction::Trunc:
2192 case Instruction::ZExt:
2193 case Instruction::SExt:
2194 case Instruction::FPTrunc:
2195 case Instruction::FPExt:
2196 case Instruction::UIToFP:
2197 case Instruction::SIToFP:
2198 case Instruction::FPToUI:
2199 case Instruction::FPToSI:
2200 return false; // These always modify bits
2201 case Instruction::BitCast:
2202 return true; // BitCast never modifies bits.
2203 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002204 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002205 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002206 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002207 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002208 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002209 }
2210}
2211
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002212/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002213bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002214 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2215}
2216
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002217/// This function determines if a pair of casts can be eliminated and what
2218/// opcode should be used in the elimination. This assumes that there are two
2219/// instructions like this:
2220/// * %F = firstOpcode SrcTy %x to MidTy
2221/// * %S = secondOpcode MidTy %F to DstTy
2222/// The function returns a resultOpcode so these two casts can be replaced with:
2223/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2224/// If no such cast is permited, the function returns 0.
2225unsigned CastInst::isEliminableCastPair(
2226 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002227 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002228 // Define the 144 possibilities for these two cast instructions. The values
2229 // in this matrix determine what to do in a given situation and select the
2230 // case in the switch below. The rows correspond to firstOp, the columns
2231 // correspond to secondOp. In looking at the table below, keep in mind
2232 // the following cast properties:
2233 //
2234 // Size Compare Source Destination
2235 // Operator Src ? Size Type Sign Type Sign
2236 // -------- ------------ ------------------- ---------------------
2237 // TRUNC > Integer Any Integral Any
2238 // ZEXT < Integral Unsigned Integer Any
2239 // SEXT < Integral Signed Integer Any
2240 // FPTOUI n/a FloatPt n/a Integral Unsigned
2241 // FPTOSI n/a FloatPt n/a Integral Signed
2242 // UITOFP n/a Integral Unsigned FloatPt n/a
2243 // SITOFP n/a Integral Signed FloatPt n/a
2244 // FPTRUNC > FloatPt n/a FloatPt n/a
2245 // FPEXT < FloatPt n/a FloatPt n/a
2246 // PTRTOINT n/a Pointer n/a Integral Unsigned
2247 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002248 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002249 //
2250 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002251 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2252 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002253 // of the produced value (we no longer know the top-part is all zeros).
2254 // Further this conversion is often much more expensive for typical hardware,
2255 // and causes issues when building libgcc. We disallow fptosi+sext for the
2256 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002257 const unsigned numCastOps =
2258 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2259 static const uint8_t CastResults[numCastOps][numCastOps] = {
2260 // T F F U S F F P I B -+
2261 // R Z S P P I I T P 2 N T |
2262 // U E E 2 2 2 2 R E I T C +- secondOp
2263 // N X X U S F F N X N 2 V |
2264 // C T T I I P P C T T P T -+
2265 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2266 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2267 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002268 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2269 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2271 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2272 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2273 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2274 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2275 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2276 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2277 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002278
2279 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002280 // merging. However, bitcast of A->B->A are allowed.
2281 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2282 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2283 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2284
2285 // Check if any of the bitcasts convert scalars<->vectors.
2286 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2287 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2288 // Unless we are bitcasing to the original type, disallow optimizations.
2289 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002290
2291 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2292 [secondOp-Instruction::CastOpsBegin];
2293 switch (ElimCase) {
2294 case 0:
2295 // categorically disallowed
2296 return 0;
2297 case 1:
2298 // allowed, use first cast's opcode
2299 return firstOp;
2300 case 2:
2301 // allowed, use second cast's opcode
2302 return secondOp;
2303 case 3:
2304 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002305 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002306 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002307 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002308 return firstOp;
2309 return 0;
2310 case 4:
2311 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002312 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002313 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314 return firstOp;
2315 return 0;
2316 case 5:
2317 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002318 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002319 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320 return secondOp;
2321 return 0;
2322 case 6:
2323 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002324 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002325 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326 return secondOp;
2327 return 0;
2328 case 7: {
2329 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002330 if (!IntPtrTy)
2331 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002332 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2333 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334 if (MidSize >= PtrSize)
2335 return Instruction::BitCast;
2336 return 0;
2337 }
2338 case 8: {
2339 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2340 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2341 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002342 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2343 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344 if (SrcSize == DstSize)
2345 return Instruction::BitCast;
2346 else if (SrcSize < DstSize)
2347 return firstOp;
2348 return secondOp;
2349 }
2350 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2351 return Instruction::ZExt;
2352 case 10:
2353 // fpext followed by ftrunc is allowed if the bit size returned to is
2354 // the same as the original, in which case its just a bitcast
2355 if (SrcTy == DstTy)
2356 return Instruction::BitCast;
2357 return 0; // If the types are not the same we can't eliminate it.
2358 case 11:
2359 // bitcast followed by ptrtoint is allowed as long as the bitcast
2360 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002361 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362 return secondOp;
2363 return 0;
2364 case 12:
2365 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002366 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367 return firstOp;
2368 return 0;
2369 case 13: {
2370 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002371 if (!IntPtrTy)
2372 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002373 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2374 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2375 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002376 if (SrcSize <= PtrSize && SrcSize == DstSize)
2377 return Instruction::BitCast;
2378 return 0;
2379 }
2380 case 99:
2381 // cast combination can't happen (error in input). This is for all cases
2382 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002383 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384 default:
Craig Topperc514b542012-02-05 22:14:15 +00002385 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387}
2388
Chris Lattner229907c2011-07-18 04:54:35 +00002389CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002390 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002391 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392 // Construct and return the appropriate CastInst subclass
2393 switch (op) {
2394 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2395 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2396 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2397 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2398 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2399 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2400 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2401 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2402 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2403 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2404 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2405 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topperc514b542012-02-05 22:14:15 +00002406 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408}
2409
Chris Lattner229907c2011-07-18 04:54:35 +00002410CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002411 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002412 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413 // Construct and return the appropriate CastInst subclass
2414 switch (op) {
2415 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2416 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2417 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2418 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2419 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2420 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2421 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2422 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2423 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2424 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2425 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2426 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topperc514b542012-02-05 22:14:15 +00002427 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429}
2430
Chris Lattner229907c2011-07-18 04:54:35 +00002431CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002432 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002433 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002434 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002435 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2436 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002437}
2438
Chris Lattner229907c2011-07-18 04:54:35 +00002439CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002440 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002441 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002442 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002443 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2444 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002445}
2446
Chris Lattner229907c2011-07-18 04:54:35 +00002447CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002448 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002449 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002450 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002451 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2452 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002453}
2454
Chris Lattner229907c2011-07-18 04:54:35 +00002455CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002456 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002457 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002458 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002459 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2460 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002461}
2462
Chris Lattner229907c2011-07-18 04:54:35 +00002463CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002464 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002465 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002466 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002467 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2468 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002469}
2470
Chris Lattner229907c2011-07-18 04:54:35 +00002471CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002472 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002473 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002474 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002475 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2476 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002477}
2478
Chris Lattner229907c2011-07-18 04:54:35 +00002479CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002480 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002481 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002482 assert(S->getType()->isPointerTy() && "Invalid cast");
2483 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002484 "Invalid cast");
2485
Duncan Sands9dff9be2010-02-15 16:12:20 +00002486 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002487 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2488 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002489}
2490
2491/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002492CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002493 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002494 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002495 assert(S->getType()->isPointerTy() && "Invalid cast");
2496 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002497 "Invalid cast");
2498
Duncan Sands9dff9be2010-02-15 16:12:20 +00002499 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002500 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2501 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002502}
2503
Chris Lattner229907c2011-07-18 04:54:35 +00002504CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002505 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002506 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002507 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002508 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002509 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2510 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002511 Instruction::CastOps opcode =
2512 (SrcBits == DstBits ? Instruction::BitCast :
2513 (SrcBits > DstBits ? Instruction::Trunc :
2514 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002515 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002516}
2517
Chris Lattner229907c2011-07-18 04:54:35 +00002518CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002519 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002520 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002521 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002522 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002523 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2524 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002525 Instruction::CastOps opcode =
2526 (SrcBits == DstBits ? Instruction::BitCast :
2527 (SrcBits > DstBits ? Instruction::Trunc :
2528 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002529 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002530}
2531
Chris Lattner229907c2011-07-18 04:54:35 +00002532CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002533 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002534 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002535 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002536 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002537 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2538 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002539 Instruction::CastOps opcode =
2540 (SrcBits == DstBits ? Instruction::BitCast :
2541 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002542 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002543}
2544
Chris Lattner229907c2011-07-18 04:54:35 +00002545CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002546 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002547 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002548 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002549 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002550 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2551 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002552 Instruction::CastOps opcode =
2553 (SrcBits == DstBits ? Instruction::BitCast :
2554 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002555 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002556}
2557
Duncan Sands55e50902008-01-06 10:12:28 +00002558// Check whether it is valid to call getCastOpcode for these types.
2559// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002560bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002561 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2562 return false;
2563
2564 if (SrcTy == DestTy)
2565 return true;
2566
Chris Lattner229907c2011-07-18 04:54:35 +00002567 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2568 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002569 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2570 // An element by element cast. Valid if casting the elements is valid.
2571 SrcTy = SrcVecTy->getElementType();
2572 DestTy = DestVecTy->getElementType();
2573 }
2574
Duncan Sands55e50902008-01-06 10:12:28 +00002575 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002576 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2577 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002578
2579 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002580 if (DestTy->isIntegerTy()) { // Casting to integral
2581 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002582 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002583 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002584 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002585 } else if (SrcTy->isVectorTy()) { // Casting from vector
2586 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002587 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002588 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002589 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002590 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2591 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002592 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002593 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002594 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002595 } else if (SrcTy->isVectorTy()) { // Casting from vector
2596 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002597 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002598 return false;
2599 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002600 } else if (DestTy->isVectorTy()) { // Casting to vector
2601 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002602 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002603 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002604 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002605 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002606 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002607 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002608 return false;
2609 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002610 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002611 if (SrcTy->isVectorTy()) {
2612 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002613 } else {
2614 return false;
2615 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002616 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002617 return false;
2618 }
2619}
2620
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621// Provide a way to get a "cast" where the cast opcode is inferred from the
2622// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002623// logic in the castIsValid function below. This axiom should hold:
2624// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2625// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002627// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002629CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002630 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2631 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002632
Duncan Sands55e50902008-01-06 10:12:28 +00002633 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2634 "Only first class types are castable!");
2635
Duncan Sandsa8514532011-05-18 07:13:41 +00002636 if (SrcTy == DestTy)
2637 return BitCast;
2638
Chris Lattner229907c2011-07-18 04:54:35 +00002639 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2640 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002641 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2642 // An element by element cast. Find the appropriate opcode based on the
2643 // element types.
2644 SrcTy = SrcVecTy->getElementType();
2645 DestTy = DestVecTy->getElementType();
2646 }
2647
2648 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002649 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2650 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002651
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002653 if (DestTy->isIntegerTy()) { // Casting to integral
2654 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002655 if (DestBits < SrcBits)
2656 return Trunc; // int -> smaller int
2657 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002658 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659 return SExt; // signed -> SEXT
2660 else
2661 return ZExt; // unsigned -> ZEXT
2662 } else {
2663 return BitCast; // Same size, No-op cast
2664 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002665 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002666 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002667 return FPToSI; // FP -> sint
2668 else
2669 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002670 } else if (SrcTy->isVectorTy()) {
2671 assert(DestBits == SrcBits &&
2672 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002673 return BitCast; // Same size, no-op cast
2674 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002675 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002676 "Casting from a value that is not first-class type");
2677 return PtrToInt; // ptr -> int
2678 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002679 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2680 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002681 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682 return SIToFP; // sint -> FP
2683 else
2684 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002685 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002686 if (DestBits < SrcBits) {
2687 return FPTrunc; // FP -> smaller FP
2688 } else if (DestBits > SrcBits) {
2689 return FPExt; // FP -> larger FP
2690 } else {
2691 return BitCast; // same size, no-op cast
2692 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002693 } else if (SrcTy->isVectorTy()) {
2694 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002695 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002696 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002697 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002698 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002699 } else if (DestTy->isVectorTy()) {
2700 assert(DestBits == SrcBits &&
2701 "Illegal cast to vector (wrong type or size)");
2702 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002703 } else if (DestTy->isPointerTy()) {
2704 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002705 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002706 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002707 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002708 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002709 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002710 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002711 if (SrcTy->isVectorTy()) {
2712 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002713 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002714 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002715 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002716 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002717 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002718}
2719
2720//===----------------------------------------------------------------------===//
2721// CastInst SubClass Constructors
2722//===----------------------------------------------------------------------===//
2723
2724/// Check that the construction parameters for a CastInst are correct. This
2725/// could be broken out into the separate constructors but it is useful to have
2726/// it in one place and to eliminate the redundant code for getting the sizes
2727/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002728bool
Chris Lattner229907c2011-07-18 04:54:35 +00002729CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002730
2731 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002732 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002733 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2734 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002735 return false;
2736
2737 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002738 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2739 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002740
Duncan Sands7f646562011-05-18 09:21:57 +00002741 // If these are vector types, get the lengths of the vectors (using zero for
2742 // scalar types means that checking that vector lengths match also checks that
2743 // scalars are not being converted to vectors or vectors to scalars).
2744 unsigned SrcLength = SrcTy->isVectorTy() ?
2745 cast<VectorType>(SrcTy)->getNumElements() : 0;
2746 unsigned DstLength = DstTy->isVectorTy() ?
2747 cast<VectorType>(DstTy)->getNumElements() : 0;
2748
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002749 // Switch on the opcode provided
2750 switch (op) {
2751 default: return false; // This is an input error
2752 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002753 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2754 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002755 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002756 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2757 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002758 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002759 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2760 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002761 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002762 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2763 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002764 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002765 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2766 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002767 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002768 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002769 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2770 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002771 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002772 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002773 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2774 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002775 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002776 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002777 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002778 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2779 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2780 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002781 return SrcTy->getScalarType()->isPointerTy() &&
2782 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002783 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002784 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002785 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002786 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2787 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2788 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002789 return SrcTy->getScalarType()->isIntegerTy() &&
2790 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002791 case Instruction::BitCast:
2792 // BitCast implies a no-op cast of type only. No bits change.
2793 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002794 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002795 return false;
2796
Duncan Sands55e50902008-01-06 10:12:28 +00002797 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002798 // these cases, the cast is okay if the source and destination bit widths
2799 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002800 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002801 }
2802}
2803
2804TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002805 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002806) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002807 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002808}
2809
2810TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002811 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002812) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002813 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002814}
2815
2816ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002817 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002818) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002819 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002820}
2821
2822ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002823 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002824) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002825 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002826}
2827SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002828 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002829) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002830 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002831}
2832
Jeff Cohencc08c832006-12-02 02:22:01 +00002833SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002834 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002835) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002836 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002837}
2838
2839FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002840 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002841) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002842 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002843}
2844
2845FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002846 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002847) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002848 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002849}
2850
2851FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002852 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002853) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002854 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002855}
2856
2857FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002858 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002859) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002860 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002861}
2862
2863UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002864 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002865) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002866 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002867}
2868
2869UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002870 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002871) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002872 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002873}
2874
2875SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002876 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002877) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002878 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002879}
2880
2881SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002882 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002883) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002884 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002885}
2886
2887FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002888 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002889) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002890 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002891}
2892
2893FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002894 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002895) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002896 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002897}
2898
2899FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002900 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002901) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002902 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002903}
2904
2905FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002906 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002907) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002908 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002909}
2910
2911PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002912 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002913) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002914 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002915}
2916
2917PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002918 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002919) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002920 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002921}
2922
2923IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002924 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002925) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002926 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002927}
2928
2929IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002930 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002931) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002932 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002933}
2934
2935BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002936 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002937) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002938 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002939}
2940
2941BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002942 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002943) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002944 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002946
2947//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002948// CmpInst Classes
2949//===----------------------------------------------------------------------===//
2950
Craig Topper3186c012012-09-23 02:12:10 +00002951void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002952
Chris Lattner229907c2011-07-18 04:54:35 +00002953CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002954 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002955 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002956 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002957 OperandTraits<CmpInst>::op_begin(this),
2958 OperandTraits<CmpInst>::operands(this),
2959 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002960 Op<0>() = LHS;
2961 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002962 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002963 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002964}
Gabor Greiff6caff662008-05-10 08:32:32 +00002965
Chris Lattner229907c2011-07-18 04:54:35 +00002966CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002967 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002968 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002969 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002970 OperandTraits<CmpInst>::op_begin(this),
2971 OperandTraits<CmpInst>::operands(this),
2972 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002973 Op<0>() = LHS;
2974 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002975 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002976 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002977}
2978
2979CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002980CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002981 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002982 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002983 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002984 if (InsertBefore)
2985 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2986 S1, S2, Name);
2987 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002988 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002989 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002990 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002991
2992 if (InsertBefore)
2993 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2994 S1, S2, Name);
2995 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002996 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002997 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002998}
2999
3000CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003001CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003002 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003003 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003004 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3005 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003006 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003007 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3008 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003009}
3010
3011void CmpInst::swapOperands() {
3012 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3013 IC->swapOperands();
3014 else
3015 cast<FCmpInst>(this)->swapOperands();
3016}
3017
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003018bool CmpInst::isCommutative() const {
3019 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003020 return IC->isCommutative();
3021 return cast<FCmpInst>(this)->isCommutative();
3022}
3023
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003024bool CmpInst::isEquality() const {
3025 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003026 return IC->isEquality();
3027 return cast<FCmpInst>(this)->isEquality();
3028}
3029
3030
Dan Gohman4e724382008-05-31 02:47:54 +00003031CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003032 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003033 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003034 case ICMP_EQ: return ICMP_NE;
3035 case ICMP_NE: return ICMP_EQ;
3036 case ICMP_UGT: return ICMP_ULE;
3037 case ICMP_ULT: return ICMP_UGE;
3038 case ICMP_UGE: return ICMP_ULT;
3039 case ICMP_ULE: return ICMP_UGT;
3040 case ICMP_SGT: return ICMP_SLE;
3041 case ICMP_SLT: return ICMP_SGE;
3042 case ICMP_SGE: return ICMP_SLT;
3043 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003044
Dan Gohman4e724382008-05-31 02:47:54 +00003045 case FCMP_OEQ: return FCMP_UNE;
3046 case FCMP_ONE: return FCMP_UEQ;
3047 case FCMP_OGT: return FCMP_ULE;
3048 case FCMP_OLT: return FCMP_UGE;
3049 case FCMP_OGE: return FCMP_ULT;
3050 case FCMP_OLE: return FCMP_UGT;
3051 case FCMP_UEQ: return FCMP_ONE;
3052 case FCMP_UNE: return FCMP_OEQ;
3053 case FCMP_UGT: return FCMP_OLE;
3054 case FCMP_ULT: return FCMP_OGE;
3055 case FCMP_UGE: return FCMP_OLT;
3056 case FCMP_ULE: return FCMP_OGT;
3057 case FCMP_ORD: return FCMP_UNO;
3058 case FCMP_UNO: return FCMP_ORD;
3059 case FCMP_TRUE: return FCMP_FALSE;
3060 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003061 }
3062}
3063
Reid Spencer266e42b2006-12-23 06:05:41 +00003064ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3065 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003066 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003067 case ICMP_EQ: case ICMP_NE:
3068 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3069 return pred;
3070 case ICMP_UGT: return ICMP_SGT;
3071 case ICMP_ULT: return ICMP_SLT;
3072 case ICMP_UGE: return ICMP_SGE;
3073 case ICMP_ULE: return ICMP_SLE;
3074 }
3075}
3076
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003077ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3078 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003079 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003080 case ICMP_EQ: case ICMP_NE:
3081 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3082 return pred;
3083 case ICMP_SGT: return ICMP_UGT;
3084 case ICMP_SLT: return ICMP_ULT;
3085 case ICMP_SGE: return ICMP_UGE;
3086 case ICMP_SLE: return ICMP_ULE;
3087 }
3088}
3089
Reid Spencer0286bc12007-02-28 22:00:54 +00003090/// Initialize a set of values that all satisfy the condition with C.
3091///
3092ConstantRange
3093ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3094 APInt Lower(C);
3095 APInt Upper(C);
3096 uint32_t BitWidth = C.getBitWidth();
3097 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003098 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00003099 case ICmpInst::ICMP_EQ: Upper++; break;
3100 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003101 case ICmpInst::ICMP_ULT:
3102 Lower = APInt::getMinValue(BitWidth);
3103 // Check for an empty-set condition.
3104 if (Lower == Upper)
3105 return ConstantRange(BitWidth, /*isFullSet=*/false);
3106 break;
3107 case ICmpInst::ICMP_SLT:
3108 Lower = APInt::getSignedMinValue(BitWidth);
3109 // Check for an empty-set condition.
3110 if (Lower == Upper)
3111 return ConstantRange(BitWidth, /*isFullSet=*/false);
3112 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003113 case ICmpInst::ICMP_UGT:
3114 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003115 // Check for an empty-set condition.
3116 if (Lower == Upper)
3117 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003118 break;
3119 case ICmpInst::ICMP_SGT:
3120 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003121 // Check for an empty-set condition.
3122 if (Lower == Upper)
3123 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003124 break;
3125 case ICmpInst::ICMP_ULE:
3126 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003127 // Check for a full-set condition.
3128 if (Lower == Upper)
3129 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003130 break;
3131 case ICmpInst::ICMP_SLE:
3132 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003133 // Check for a full-set condition.
3134 if (Lower == Upper)
3135 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003136 break;
3137 case ICmpInst::ICMP_UGE:
3138 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003139 // Check for a full-set condition.
3140 if (Lower == Upper)
3141 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003142 break;
3143 case ICmpInst::ICMP_SGE:
3144 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003145 // Check for a full-set condition.
3146 if (Lower == Upper)
3147 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003148 break;
3149 }
3150 return ConstantRange(Lower, Upper);
3151}
3152
Dan Gohman4e724382008-05-31 02:47:54 +00003153CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003154 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003155 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003156 case ICMP_EQ: case ICMP_NE:
3157 return pred;
3158 case ICMP_SGT: return ICMP_SLT;
3159 case ICMP_SLT: return ICMP_SGT;
3160 case ICMP_SGE: return ICMP_SLE;
3161 case ICMP_SLE: return ICMP_SGE;
3162 case ICMP_UGT: return ICMP_ULT;
3163 case ICMP_ULT: return ICMP_UGT;
3164 case ICMP_UGE: return ICMP_ULE;
3165 case ICMP_ULE: return ICMP_UGE;
3166
Reid Spencerd9436b62006-11-20 01:22:35 +00003167 case FCMP_FALSE: case FCMP_TRUE:
3168 case FCMP_OEQ: case FCMP_ONE:
3169 case FCMP_UEQ: case FCMP_UNE:
3170 case FCMP_ORD: case FCMP_UNO:
3171 return pred;
3172 case FCMP_OGT: return FCMP_OLT;
3173 case FCMP_OLT: return FCMP_OGT;
3174 case FCMP_OGE: return FCMP_OLE;
3175 case FCMP_OLE: return FCMP_OGE;
3176 case FCMP_UGT: return FCMP_ULT;
3177 case FCMP_ULT: return FCMP_UGT;
3178 case FCMP_UGE: return FCMP_ULE;
3179 case FCMP_ULE: return FCMP_UGE;
3180 }
3181}
3182
Reid Spencer266e42b2006-12-23 06:05:41 +00003183bool CmpInst::isUnsigned(unsigned short predicate) {
3184 switch (predicate) {
3185 default: return false;
3186 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3187 case ICmpInst::ICMP_UGE: return true;
3188 }
3189}
3190
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003191bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003192 switch (predicate) {
3193 default: return false;
3194 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3195 case ICmpInst::ICMP_SGE: return true;
3196 }
3197}
3198
3199bool CmpInst::isOrdered(unsigned short predicate) {
3200 switch (predicate) {
3201 default: return false;
3202 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3203 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3204 case FCmpInst::FCMP_ORD: return true;
3205 }
3206}
3207
3208bool CmpInst::isUnordered(unsigned short predicate) {
3209 switch (predicate) {
3210 default: return false;
3211 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3212 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3213 case FCmpInst::FCMP_UNO: return true;
3214 }
3215}
3216
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003217bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3218 switch(predicate) {
3219 default: return false;
3220 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3221 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3222 }
3223}
3224
3225bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3226 switch(predicate) {
3227 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3228 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3229 default: return false;
3230 }
3231}
3232
3233
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003234//===----------------------------------------------------------------------===//
3235// SwitchInst Implementation
3236//===----------------------------------------------------------------------===//
3237
Chris Lattnerbaf00152010-11-17 05:41:46 +00003238void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3239 assert(Value && Default && NumReserved);
3240 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003241 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003242 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003243
Gabor Greif2d3024d2008-05-26 21:33:52 +00003244 OperandList[0] = Value;
3245 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003246}
3247
Chris Lattner2195fc42007-02-24 00:55:48 +00003248/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3249/// switch on and a default destination. The number of additional cases can
3250/// be specified here to make memory allocation more efficient. This
3251/// constructor can also autoinsert before another instruction.
3252SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3253 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003254 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3255 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003256 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003257}
3258
3259/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3260/// switch on and a default destination. The number of additional cases can
3261/// be specified here to make memory allocation more efficient. This
3262/// constructor also autoinserts at the end of the specified BasicBlock.
3263SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3264 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003265 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3266 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003267 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003268}
3269
Misha Brukmanb1c93172005-04-21 23:48:37 +00003270SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003271 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3272 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3273 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003274 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003275 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003276 OL[i] = InOL[i];
3277 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003278 }
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003279 TheSubsets = SI.TheSubsets;
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003280 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003281}
3282
Gordon Henriksen14a55692007-12-10 02:14:30 +00003283SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003284 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003285}
3286
3287
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003288/// addCase - Add an entry to the switch instruction...
3289///
Chris Lattner47ac1872005-02-24 05:32:09 +00003290void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003291 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00003292
3293 // FIXME: Currently we work with ConstantInt based cases.
3294 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003295 Mapping.add(IntItem::fromConstantInt(OnVal));
3296 IntegersSubset CaseRanges = Mapping.getCase();
3297 addCase(CaseRanges, Dest);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00003298}
3299
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003300void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003301 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003302 unsigned OpNo = NumOperands;
3303 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003304 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003305 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003306 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003307 NumOperands = OpNo+2;
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003308
3309 SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3310
3311 CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3312 Case.updateCaseValueOperand(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003313 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003314}
3315
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003316/// removeCase - This method removes the specified case and its successor
3317/// from the switch instruction.
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003318void SwitchInst::removeCase(CaseIt& i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003319 unsigned idx = i.getCaseIndex();
3320
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003321 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003322
3323 unsigned NumOps = getNumOperands();
3324 Use *OL = OperandList;
3325
Jay Foad14277722011-02-01 09:22:34 +00003326 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003327 if (2 + (idx + 1) * 2 != NumOps) {
3328 OL[2 + idx * 2] = OL[NumOps - 2];
3329 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003330 }
3331
3332 // Nuke the last value.
3333 OL[NumOps-2].set(0);
3334 OL[NumOps-2+1].set(0);
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003335
3336 // Do the same with TheCases collection:
3337 if (i.SubsetIt != --TheSubsets.end()) {
3338 *i.SubsetIt = TheSubsets.back();
3339 TheSubsets.pop_back();
3340 } else {
3341 TheSubsets.pop_back();
3342 i.SubsetIt = TheSubsets.end();
3343 }
3344
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003345 NumOperands = NumOps-2;
3346}
3347
Jay Foade98f29d2011-04-01 08:00:58 +00003348/// growOperands - grow operands - This grows the operand list in response
3349/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003350///
Jay Foade98f29d2011-04-01 08:00:58 +00003351void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003352 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003353 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003354
3355 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003356 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003357 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003358 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003359 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003360 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003361 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003362 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003363}
3364
3365
3366BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3367 return getSuccessor(idx);
3368}
3369unsigned SwitchInst::getNumSuccessorsV() const {
3370 return getNumSuccessors();
3371}
3372void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3373 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003374}
Chris Lattnerf22be932004-10-15 23:52:53 +00003375
Chris Lattner3ed871f2009-10-27 19:13:16 +00003376//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003377// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003378//===----------------------------------------------------------------------===//
3379
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003380void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003381 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003382 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003383 ReservedSpace = 1+NumDests;
3384 NumOperands = 1;
3385 OperandList = allocHungoffUses(ReservedSpace);
3386
3387 OperandList[0] = Address;
3388}
3389
3390
Jay Foade98f29d2011-04-01 08:00:58 +00003391/// growOperands - grow operands - This grows the operand list in response
3392/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003393///
Jay Foade98f29d2011-04-01 08:00:58 +00003394void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003395 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003396 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003397
3398 ReservedSpace = NumOps;
3399 Use *NewOps = allocHungoffUses(NumOps);
3400 Use *OldOps = OperandList;
3401 for (unsigned i = 0; i != e; ++i)
3402 NewOps[i] = OldOps[i];
3403 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003404 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003405}
3406
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003407IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3408 Instruction *InsertBefore)
3409: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003410 0, 0, InsertBefore) {
3411 init(Address, NumCases);
3412}
3413
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003414IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3415 BasicBlock *InsertAtEnd)
3416: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003417 0, 0, InsertAtEnd) {
3418 init(Address, NumCases);
3419}
3420
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003421IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3422 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003423 allocHungoffUses(IBI.getNumOperands()),
3424 IBI.getNumOperands()) {
3425 Use *OL = OperandList, *InOL = IBI.OperandList;
3426 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3427 OL[i] = InOL[i];
3428 SubclassOptionalData = IBI.SubclassOptionalData;
3429}
3430
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003431IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003432 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003433}
3434
3435/// addDestination - Add a destination.
3436///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003437void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003438 unsigned OpNo = NumOperands;
3439 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003440 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003441 // Initialize some new operands.
3442 assert(OpNo < ReservedSpace && "Growing didn't work!");
3443 NumOperands = OpNo+1;
3444 OperandList[OpNo] = DestBB;
3445}
3446
3447/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003448/// indirectbr instruction.
3449void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003450 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3451
3452 unsigned NumOps = getNumOperands();
3453 Use *OL = OperandList;
3454
3455 // Replace this value with the last one.
3456 OL[idx+1] = OL[NumOps-1];
3457
3458 // Nuke the last value.
3459 OL[NumOps-1].set(0);
3460 NumOperands = NumOps-1;
3461}
3462
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003463BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003464 return getSuccessor(idx);
3465}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003466unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003467 return getNumSuccessors();
3468}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003469void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003470 setSuccessor(idx, B);
3471}
3472
3473//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003474// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003475//===----------------------------------------------------------------------===//
3476
Chris Lattnerf22be932004-10-15 23:52:53 +00003477// Define these methods here so vtables don't get emitted into every translation
3478// unit that uses these classes.
3479
Devang Patel11cf3f42009-10-27 22:16:29 +00003480GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3481 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003482}
3483
Devang Patel11cf3f42009-10-27 22:16:29 +00003484BinaryOperator *BinaryOperator::clone_impl() const {
3485 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003486}
3487
Devang Patel11cf3f42009-10-27 22:16:29 +00003488FCmpInst* FCmpInst::clone_impl() const {
3489 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003490}
3491
Devang Patel11cf3f42009-10-27 22:16:29 +00003492ICmpInst* ICmpInst::clone_impl() const {
3493 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003494}
3495
Devang Patel11cf3f42009-10-27 22:16:29 +00003496ExtractValueInst *ExtractValueInst::clone_impl() const {
3497 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003498}
3499
Devang Patel11cf3f42009-10-27 22:16:29 +00003500InsertValueInst *InsertValueInst::clone_impl() const {
3501 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003502}
3503
Devang Patel11cf3f42009-10-27 22:16:29 +00003504AllocaInst *AllocaInst::clone_impl() const {
3505 return new AllocaInst(getAllocatedType(),
3506 (Value*)getOperand(0),
3507 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003508}
3509
Devang Patel11cf3f42009-10-27 22:16:29 +00003510LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003511 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3512 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003513}
3514
Devang Patel11cf3f42009-10-27 22:16:29 +00003515StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003516 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003517 getAlignment(), getOrdering(), getSynchScope());
3518
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003519}
3520
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003521AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3522 AtomicCmpXchgInst *Result =
3523 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3524 getOrdering(), getSynchScope());
3525 Result->setVolatile(isVolatile());
3526 return Result;
3527}
3528
3529AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3530 AtomicRMWInst *Result =
3531 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3532 getOrdering(), getSynchScope());
3533 Result->setVolatile(isVolatile());
3534 return Result;
3535}
3536
Eli Friedmanfee02c62011-07-25 23:16:38 +00003537FenceInst *FenceInst::clone_impl() const {
3538 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3539}
3540
Devang Patel11cf3f42009-10-27 22:16:29 +00003541TruncInst *TruncInst::clone_impl() const {
3542 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003543}
3544
Devang Patel11cf3f42009-10-27 22:16:29 +00003545ZExtInst *ZExtInst::clone_impl() const {
3546 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003547}
3548
Devang Patel11cf3f42009-10-27 22:16:29 +00003549SExtInst *SExtInst::clone_impl() const {
3550 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003551}
3552
Devang Patel11cf3f42009-10-27 22:16:29 +00003553FPTruncInst *FPTruncInst::clone_impl() const {
3554 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003555}
3556
Devang Patel11cf3f42009-10-27 22:16:29 +00003557FPExtInst *FPExtInst::clone_impl() const {
3558 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003559}
3560
Devang Patel11cf3f42009-10-27 22:16:29 +00003561UIToFPInst *UIToFPInst::clone_impl() const {
3562 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003563}
3564
Devang Patel11cf3f42009-10-27 22:16:29 +00003565SIToFPInst *SIToFPInst::clone_impl() const {
3566 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003567}
3568
Devang Patel11cf3f42009-10-27 22:16:29 +00003569FPToUIInst *FPToUIInst::clone_impl() const {
3570 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003571}
3572
Devang Patel11cf3f42009-10-27 22:16:29 +00003573FPToSIInst *FPToSIInst::clone_impl() const {
3574 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003575}
3576
Devang Patel11cf3f42009-10-27 22:16:29 +00003577PtrToIntInst *PtrToIntInst::clone_impl() const {
3578 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003579}
3580
Devang Patel11cf3f42009-10-27 22:16:29 +00003581IntToPtrInst *IntToPtrInst::clone_impl() const {
3582 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003583}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003584
Devang Patel11cf3f42009-10-27 22:16:29 +00003585BitCastInst *BitCastInst::clone_impl() const {
3586 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003587}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003588
Devang Patel11cf3f42009-10-27 22:16:29 +00003589CallInst *CallInst::clone_impl() const {
3590 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003591}
3592
Devang Patel11cf3f42009-10-27 22:16:29 +00003593SelectInst *SelectInst::clone_impl() const {
3594 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003595}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003596
Devang Patel11cf3f42009-10-27 22:16:29 +00003597VAArgInst *VAArgInst::clone_impl() const {
3598 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003599}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003600
Devang Patel11cf3f42009-10-27 22:16:29 +00003601ExtractElementInst *ExtractElementInst::clone_impl() const {
3602 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003603}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003604
Devang Patel11cf3f42009-10-27 22:16:29 +00003605InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003606 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003607}
3608
Devang Patel11cf3f42009-10-27 22:16:29 +00003609ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003610 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003611}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003612
Devang Patel11cf3f42009-10-27 22:16:29 +00003613PHINode *PHINode::clone_impl() const {
3614 return new PHINode(*this);
3615}
3616
Bill Wendlingfae14752011-08-12 20:24:12 +00003617LandingPadInst *LandingPadInst::clone_impl() const {
3618 return new LandingPadInst(*this);
3619}
3620
Devang Patel11cf3f42009-10-27 22:16:29 +00003621ReturnInst *ReturnInst::clone_impl() const {
3622 return new(getNumOperands()) ReturnInst(*this);
3623}
3624
3625BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003626 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003627}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003628
Devang Patel11cf3f42009-10-27 22:16:29 +00003629SwitchInst *SwitchInst::clone_impl() const {
3630 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003631}
3632
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003633IndirectBrInst *IndirectBrInst::clone_impl() const {
3634 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003635}
3636
3637
Devang Patel11cf3f42009-10-27 22:16:29 +00003638InvokeInst *InvokeInst::clone_impl() const {
3639 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003640}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003641
Bill Wendlingf891bf82011-07-31 06:30:59 +00003642ResumeInst *ResumeInst::clone_impl() const {
3643 return new(1) ResumeInst(*this);
3644}
3645
Devang Patel11cf3f42009-10-27 22:16:29 +00003646UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003647 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003648 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003649}