blob: e3cbf220d4a3a97ed75c1a1a754a2392ad76be28 [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
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000401bool CallInst::paramHasNoCaptureAttr(unsigned i) const {
402 if (AttributeList.getParamAttributes(i).hasNoCaptureAttr())
403 return true;
404 if (const Function *F = getCalledFunction())
405 return F->getParamAttributes(i).hasNoCaptureAttr();
406 return false;
407}
408
Devang Patelba3fa6c2008-09-23 23:03:40 +0000409bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000410 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000411 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000412 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000413 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000414 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000415}
416
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000417/// IsConstantOne - Return true only if val is constant int 1
418static bool IsConstantOne(Value *val) {
419 assert(val && "IsConstantOne does not work with NULL val");
420 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
421}
422
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000423static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000424 BasicBlock *InsertAtEnd, Type *IntPtrTy,
425 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000426 Value *ArraySize, Function *MallocF,
427 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000428 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000429 "createMalloc needs either InsertBefore or InsertAtEnd");
430
431 // malloc(type) becomes:
432 // bitcast (i8* malloc(typeSize)) to type*
433 // malloc(type, arraySize) becomes:
434 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000435 if (!ArraySize)
436 ArraySize = ConstantInt::get(IntPtrTy, 1);
437 else if (ArraySize->getType() != IntPtrTy) {
438 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000439 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
440 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000441 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000442 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
443 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000444 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000446 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000447 if (IsConstantOne(AllocSize)) {
448 AllocSize = ArraySize; // Operand * 1 = Operand
449 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
450 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
451 false /*ZExt*/);
452 // Malloc arg is constant product of type size and array size
453 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
454 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000455 // Multiply type size by the array size...
456 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000457 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
458 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000459 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000460 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
461 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000462 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000463 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000464
Victor Hernandez788eaab2009-09-18 19:20:02 +0000465 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000466 // Create the call to Malloc.
467 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
468 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000469 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000470 Value *MallocFunc = MallocF;
471 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000472 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000473 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000474 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000475 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000476 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000477 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000478 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000479 Result = MCall;
480 if (Result->getType() != AllocPtrType)
481 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000482 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000483 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000484 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000485 Result = MCall;
486 if (Result->getType() != AllocPtrType) {
487 InsertAtEnd->getInstList().push_back(MCall);
488 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000489 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000490 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000491 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000492 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000493 if (Function *F = dyn_cast<Function>(MallocFunc)) {
494 MCall->setCallingConv(F->getCallingConv());
495 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
496 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000497 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000498
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000499 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000500}
501
502/// CreateMalloc - Generate the IR for a call to malloc:
503/// 1. Compute the malloc call's argument as the specified type's size,
504/// possibly multiplied by the array size if the array size is not
505/// constant 1.
506/// 2. Call malloc with that argument.
507/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000508Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000509 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000510 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000511 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000512 const Twine &Name) {
513 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000514 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000515}
516
517/// CreateMalloc - Generate the IR for a call to malloc:
518/// 1. Compute the malloc call's argument as the specified type's size,
519/// possibly multiplied by the array size if the array size is not
520/// constant 1.
521/// 2. Call malloc with that argument.
522/// 3. Bitcast the result of the malloc call to the specified type.
523/// Note: This function does not add the bitcast to the basic block, that is the
524/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000525Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000526 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000527 Value *AllocSize, Value *ArraySize,
528 Function *MallocF, const Twine &Name) {
529 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000530 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000531}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000532
Victor Hernandeze2971492009-10-24 04:23:03 +0000533static Instruction* createFree(Value* Source, Instruction *InsertBefore,
534 BasicBlock *InsertAtEnd) {
535 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
536 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000537 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000538 "Can not free something of nonpointer type!");
539
540 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
541 Module* M = BB->getParent()->getParent();
542
Chris Lattner229907c2011-07-18 04:54:35 +0000543 Type *VoidTy = Type::getVoidTy(M->getContext());
544 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000545 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000546 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000547 CallInst* Result = NULL;
548 Value *PtrCast = Source;
549 if (InsertBefore) {
550 if (Source->getType() != IntPtrTy)
551 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
552 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
553 } else {
554 if (Source->getType() != IntPtrTy)
555 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
556 Result = CallInst::Create(FreeFunc, PtrCast, "");
557 }
558 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000559 if (Function *F = dyn_cast<Function>(FreeFunc))
560 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000561
562 return Result;
563}
564
565/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000566Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
567 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000568}
569
570/// CreateFree - Generate the IR for a call to the builtin free function.
571/// Note: This function does not add the call to the basic block, that is the
572/// responsibility of the caller.
573Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
574 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
575 assert(FreeCall && "CreateFree did not create a CallInst");
576 return FreeCall;
577}
578
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000579//===----------------------------------------------------------------------===//
580// InvokeInst Implementation
581//===----------------------------------------------------------------------===//
582
583void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000584 ArrayRef<Value *> Args, const Twine &NameStr) {
585 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000586 Op<-3>() = Fn;
587 Op<-2>() = IfNormal;
588 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000589
590#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000591 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000592 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000593
Jay Foad5bd375a2011-07-15 08:37:34 +0000594 assert(((Args.size() == FTy->getNumParams()) ||
595 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000596 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000597
Jay Foad5bd375a2011-07-15 08:37:34 +0000598 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000599 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000600 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000601 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000602#endif
603
604 std::copy(Args.begin(), Args.end(), op_begin());
605 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000606}
607
Misha Brukmanb1c93172005-04-21 23:48:37 +0000608InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000609 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000610 OperandTraits<InvokeInst>::op_end(this)
611 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000612 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000613 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000614 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000615 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000616 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000617}
618
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000619BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
620 return getSuccessor(idx);
621}
622unsigned InvokeInst::getNumSuccessorsV() const {
623 return getNumSuccessors();
624}
625void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
626 return setSuccessor(idx, B);
627}
628
Bill Wendling8baa61d2012-10-03 17:54:26 +0000629bool InvokeInst::paramHasSExtAttr(unsigned i) const {
630 if (AttributeList.getParamAttributes(i).hasSExtAttr())
631 return true;
632 if (const Function *F = getCalledFunction())
633 return F->getParamAttributes(i).hasSExtAttr();
634 return false;
635}
636
637bool InvokeInst::paramHasZExtAttr(unsigned i) const {
638 if (AttributeList.getParamAttributes(i).hasZExtAttr())
639 return true;
640 if (const Function *F = getCalledFunction())
641 return F->getParamAttributes(i).hasZExtAttr();
642 return false;
643}
644
645bool InvokeInst::paramHasInRegAttr(unsigned i) const {
646 if (AttributeList.getParamAttributes(i).hasInRegAttr())
647 return true;
648 if (const Function *F = getCalledFunction())
649 return F->getParamAttributes(i).hasInRegAttr();
650 return false;
651}
652
653bool InvokeInst::paramHasStructRetAttr(unsigned i) const {
654 if (AttributeList.getParamAttributes(i).hasStructRetAttr())
655 return true;
656 if (const Function *F = getCalledFunction())
657 return F->getParamAttributes(i).hasStructRetAttr();
658 return false;
659}
660
661bool InvokeInst::paramHasNestAttr(unsigned i) const {
662 if (AttributeList.getParamAttributes(i).hasNestAttr())
663 return true;
664 if (const Function *F = getCalledFunction())
665 return F->getParamAttributes(i).hasNestAttr();
666 return false;
667}
668
669bool InvokeInst::paramHasByValAttr(unsigned i) const {
670 if (AttributeList.getParamAttributes(i).hasByValAttr())
671 return true;
672 if (const Function *F = getCalledFunction())
673 return F->getParamAttributes(i).hasByValAttr();
674 return false;
675}
676
Bill Wendlingd7773982012-10-04 06:52:09 +0000677bool InvokeInst::paramHasNoAliasAttr(unsigned i) const {
678 if (AttributeList.getParamAttributes(i).hasNoAliasAttr())
679 return true;
680 if (const Function *F = getCalledFunction())
681 return F->getParamAttributes(i).hasNoAliasAttr();
682 return false;
683}
684
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000685bool InvokeInst::paramHasNoCaptureAttr(unsigned i) const {
686 if (AttributeList.getParamAttributes(i).hasNoCaptureAttr())
687 return true;
688 if (const Function *F = getCalledFunction())
689 return F->getParamAttributes(i).hasNoCaptureAttr();
690 return false;
691}
692
Devang Patelba3fa6c2008-09-23 23:03:40 +0000693bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000694 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000695 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000696 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000697 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000698 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000699}
700
Devang Patel4c758ea2008-09-25 21:00:45 +0000701void InvokeInst::addAttribute(unsigned i, Attributes attr) {
702 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000703 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000704 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000705}
706
Devang Patel4c758ea2008-09-25 21:00:45 +0000707void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
708 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000709 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000710 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000711}
712
Bill Wendlingfae14752011-08-12 20:24:12 +0000713LandingPadInst *InvokeInst::getLandingPadInst() const {
714 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
715}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000716
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000717//===----------------------------------------------------------------------===//
718// ReturnInst Implementation
719//===----------------------------------------------------------------------===//
720
Chris Lattner2195fc42007-02-24 00:55:48 +0000721ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000722 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000723 OperandTraits<ReturnInst>::op_end(this) -
724 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000725 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000726 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000727 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000728 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000729}
730
Owen Anderson55f1c092009-08-13 21:58:54 +0000731ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
732 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000733 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
734 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000735 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000736 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000737}
Owen Anderson55f1c092009-08-13 21:58:54 +0000738ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
739 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000740 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
741 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000742 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000743 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000744}
Owen Anderson55f1c092009-08-13 21:58:54 +0000745ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
746 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000747 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000748}
749
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000750unsigned ReturnInst::getNumSuccessorsV() const {
751 return getNumSuccessors();
752}
753
Devang Patelae682fb2008-02-26 17:56:20 +0000754/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
755/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000756void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000757 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000758}
759
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000760BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000761 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000762}
763
Devang Patel59643e52008-02-23 00:35:18 +0000764ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000765}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000766
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000767//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000768// ResumeInst Implementation
769//===----------------------------------------------------------------------===//
770
771ResumeInst::ResumeInst(const ResumeInst &RI)
772 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
773 OperandTraits<ResumeInst>::op_begin(this), 1) {
774 Op<0>() = RI.Op<0>();
775}
776
777ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
778 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
779 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
780 Op<0>() = Exn;
781}
782
783ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
784 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
785 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
786 Op<0>() = Exn;
787}
788
789unsigned ResumeInst::getNumSuccessorsV() const {
790 return getNumSuccessors();
791}
792
793void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
794 llvm_unreachable("ResumeInst has no successors!");
795}
796
797BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
798 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000799}
800
801//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000802// UnreachableInst Implementation
803//===----------------------------------------------------------------------===//
804
Owen Anderson55f1c092009-08-13 21:58:54 +0000805UnreachableInst::UnreachableInst(LLVMContext &Context,
806 Instruction *InsertBefore)
807 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
808 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000809}
Owen Anderson55f1c092009-08-13 21:58:54 +0000810UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
811 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
812 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000813}
814
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000815unsigned UnreachableInst::getNumSuccessorsV() const {
816 return getNumSuccessors();
817}
818
819void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000820 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000821}
822
823BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000824 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000825}
826
827//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828// BranchInst Implementation
829//===----------------------------------------------------------------------===//
830
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000831void BranchInst::AssertOK() {
832 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000833 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000834 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000835}
836
Chris Lattner2195fc42007-02-24 00:55:48 +0000837BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000838 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000839 OperandTraits<BranchInst>::op_end(this) - 1,
840 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000841 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000842 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000843}
844BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
845 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000846 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000847 OperandTraits<BranchInst>::op_end(this) - 3,
848 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000849 Op<-1>() = IfTrue;
850 Op<-2>() = IfFalse;
851 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000852#ifndef NDEBUG
853 AssertOK();
854#endif
855}
856
857BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000858 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000859 OperandTraits<BranchInst>::op_end(this) - 1,
860 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000861 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000862 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000863}
864
865BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
866 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000867 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000868 OperandTraits<BranchInst>::op_end(this) - 3,
869 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000870 Op<-1>() = IfTrue;
871 Op<-2>() = IfFalse;
872 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000873#ifndef NDEBUG
874 AssertOK();
875#endif
876}
877
878
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000879BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000880 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000881 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
882 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000883 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000884 if (BI.getNumOperands() != 1) {
885 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000886 Op<-3>() = BI.Op<-3>();
887 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000888 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000889 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000890}
891
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000892void BranchInst::swapSuccessors() {
893 assert(isConditional() &&
894 "Cannot swap successors of an unconditional branch");
895 Op<-1>().swap(Op<-2>());
896
897 // Update profile metadata if present and it matches our structural
898 // expectations.
899 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
900 if (!ProfileData || ProfileData->getNumOperands() != 3)
901 return;
902
903 // The first operand is the name. Fetch them backwards and build a new one.
904 Value *Ops[] = {
905 ProfileData->getOperand(0),
906 ProfileData->getOperand(2),
907 ProfileData->getOperand(1)
908 };
909 setMetadata(LLVMContext::MD_prof,
910 MDNode::get(ProfileData->getContext(), Ops));
911}
912
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000913BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
914 return getSuccessor(idx);
915}
916unsigned BranchInst::getNumSuccessorsV() const {
917 return getNumSuccessors();
918}
919void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
920 setSuccessor(idx, B);
921}
922
923
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000924//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000925// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000926//===----------------------------------------------------------------------===//
927
Owen Andersonb6b25302009-07-14 23:09:55 +0000928static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000929 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000930 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000931 else {
932 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000933 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000934 assert(Amt->getType()->isIntegerTy() &&
935 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000936 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000937 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000938}
939
Chris Lattner229907c2011-07-18 04:54:35 +0000940AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000941 const Twine &Name, Instruction *InsertBefore)
942 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
943 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
944 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000945 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000946 setName(Name);
947}
948
Chris Lattner229907c2011-07-18 04:54:35 +0000949AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000950 const Twine &Name, BasicBlock *InsertAtEnd)
951 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
952 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
953 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000954 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000955 setName(Name);
956}
957
Chris Lattner229907c2011-07-18 04:54:35 +0000958AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000959 Instruction *InsertBefore)
960 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
961 getAISize(Ty->getContext(), 0), InsertBefore) {
962 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000963 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000964 setName(Name);
965}
966
Chris Lattner229907c2011-07-18 04:54:35 +0000967AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000968 BasicBlock *InsertAtEnd)
969 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
970 getAISize(Ty->getContext(), 0), InsertAtEnd) {
971 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000972 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000973 setName(Name);
974}
975
Chris Lattner229907c2011-07-18 04:54:35 +0000976AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000977 const Twine &Name, Instruction *InsertBefore)
978 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000979 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000980 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000981 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000982 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000983}
984
Chris Lattner229907c2011-07-18 04:54:35 +0000985AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000986 const Twine &Name, BasicBlock *InsertAtEnd)
987 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000988 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000989 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000990 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000991 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000992}
993
Gordon Henriksen14a55692007-12-10 02:14:30 +0000994// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000995AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000996}
997
Victor Hernandez8acf2952009-10-23 21:09:37 +0000998void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000999 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001000 assert(Align <= MaximumAlignment &&
1001 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001002 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +00001003 assert(getAlignment() == Align && "Alignment representation error!");
1004}
1005
Victor Hernandez8acf2952009-10-23 21:09:37 +00001006bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001007 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001008 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001009 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001010}
1011
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001012Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001013 return getType()->getElementType();
1014}
1015
Chris Lattner8b291e62008-11-26 02:54:17 +00001016/// isStaticAlloca - Return true if this alloca is in the entry block of the
1017/// function and is a constant size. If so, the code generator will fold it
1018/// into the prolog/epilog code, so it is basically free.
1019bool AllocaInst::isStaticAlloca() const {
1020 // Must be constant size.
1021 if (!isa<ConstantInt>(getArraySize())) return false;
1022
1023 // Must be in the entry block.
1024 const BasicBlock *Parent = getParent();
1025 return Parent == &Parent->getParent()->front();
1026}
1027
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001028//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001029// LoadInst Implementation
1030//===----------------------------------------------------------------------===//
1031
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001032void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001033 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001035 assert(!(isAtomic() && getAlignment() == 0) &&
1036 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001037}
1038
Daniel Dunbar4975db62009-07-25 04:41:11 +00001039LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001040 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001041 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001042 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001043 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001044 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001045 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001046 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001047}
1048
Daniel Dunbar4975db62009-07-25 04:41:11 +00001049LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001050 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001051 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001052 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001053 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001054 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001055 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001056 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001057}
1058
Daniel Dunbar4975db62009-07-25 04:41:11 +00001059LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001060 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001061 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001062 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001063 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001064 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001065 setAtomic(NotAtomic);
1066 AssertOK();
1067 setName(Name);
1068}
1069
1070LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1071 BasicBlock *InsertAE)
1072 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1073 Load, Ptr, InsertAE) {
1074 setVolatile(isVolatile);
1075 setAlignment(0);
1076 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001077 AssertOK();
1078 setName(Name);
1079}
1080
Daniel Dunbar4975db62009-07-25 04:41:11 +00001081LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001082 unsigned Align, Instruction *InsertBef)
1083 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1084 Load, Ptr, InsertBef) {
1085 setVolatile(isVolatile);
1086 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001087 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001088 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001089 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001090}
1091
Daniel Dunbar4975db62009-07-25 04:41:11 +00001092LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001093 unsigned Align, BasicBlock *InsertAE)
1094 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1095 Load, Ptr, InsertAE) {
1096 setVolatile(isVolatile);
1097 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001098 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +00001099 AssertOK();
1100 setName(Name);
1101}
1102
Eli Friedman59b66882011-08-09 23:02:53 +00001103LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1104 unsigned Align, AtomicOrdering Order,
1105 SynchronizationScope SynchScope,
1106 Instruction *InsertBef)
1107 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1108 Load, Ptr, InsertBef) {
1109 setVolatile(isVolatile);
1110 setAlignment(Align);
1111 setAtomic(Order, SynchScope);
1112 AssertOK();
1113 setName(Name);
1114}
1115
1116LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1117 unsigned Align, AtomicOrdering Order,
1118 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001119 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001120 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001121 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001122 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001123 setAlignment(Align);
1124 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001125 AssertOK();
1126 setName(Name);
1127}
1128
Daniel Dunbar27096822009-08-11 18:11:15 +00001129LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1130 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1131 Load, Ptr, InsertBef) {
1132 setVolatile(false);
1133 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001134 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001135 AssertOK();
1136 if (Name && Name[0]) setName(Name);
1137}
1138
1139LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1140 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1141 Load, Ptr, InsertAE) {
1142 setVolatile(false);
1143 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001144 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001145 AssertOK();
1146 if (Name && Name[0]) setName(Name);
1147}
1148
1149LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1150 Instruction *InsertBef)
1151: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1152 Load, Ptr, InsertBef) {
1153 setVolatile(isVolatile);
1154 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001155 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001156 AssertOK();
1157 if (Name && Name[0]) setName(Name);
1158}
1159
1160LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1161 BasicBlock *InsertAE)
1162 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1163 Load, Ptr, InsertAE) {
1164 setVolatile(isVolatile);
1165 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001166 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001167 AssertOK();
1168 if (Name && Name[0]) setName(Name);
1169}
1170
Christopher Lamb84485702007-04-22 19:24:39 +00001171void LoadInst::setAlignment(unsigned Align) {
1172 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001173 assert(Align <= MaximumAlignment &&
1174 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001175 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001176 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001177 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001178}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001179
1180//===----------------------------------------------------------------------===//
1181// StoreInst Implementation
1182//===----------------------------------------------------------------------===//
1183
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001184void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001185 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001186 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001187 "Ptr must have pointer type!");
1188 assert(getOperand(0)->getType() ==
1189 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001190 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001191 assert(!(isAtomic() && getAlignment() == 0) &&
1192 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001193}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001194
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001195
1196StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001197 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001198 OperandTraits<StoreInst>::op_begin(this),
1199 OperandTraits<StoreInst>::operands(this),
1200 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001201 Op<0>() = val;
1202 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001203 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001204 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001205 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001206 AssertOK();
1207}
1208
1209StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001210 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001211 OperandTraits<StoreInst>::op_begin(this),
1212 OperandTraits<StoreInst>::operands(this),
1213 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001214 Op<0>() = val;
1215 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001216 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001217 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001218 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001219 AssertOK();
1220}
1221
Misha Brukmanb1c93172005-04-21 23:48:37 +00001222StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001223 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001224 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001225 OperandTraits<StoreInst>::op_begin(this),
1226 OperandTraits<StoreInst>::operands(this),
1227 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001228 Op<0>() = val;
1229 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001230 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001231 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001232 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001233 AssertOK();
1234}
1235
1236StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1237 unsigned Align, 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),
1241 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001242 Op<0>() = val;
1243 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001244 setVolatile(isVolatile);
1245 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001246 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001247 AssertOK();
1248}
1249
Misha Brukmanb1c93172005-04-21 23:48:37 +00001250StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001251 unsigned Align, AtomicOrdering Order,
1252 SynchronizationScope SynchScope,
1253 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001254 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001255 OperandTraits<StoreInst>::op_begin(this),
1256 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001257 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001258 Op<0>() = val;
1259 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001260 setVolatile(isVolatile);
1261 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001262 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001263 AssertOK();
1264}
1265
1266StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001267 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001268 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001269 OperandTraits<StoreInst>::op_begin(this),
1270 OperandTraits<StoreInst>::operands(this),
1271 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001272 Op<0>() = val;
1273 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001274 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001275 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001276 setAtomic(NotAtomic);
1277 AssertOK();
1278}
1279
1280StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1281 unsigned Align, 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(NotAtomic);
1291 AssertOK();
1292}
1293
1294StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1295 unsigned Align, AtomicOrdering Order,
1296 SynchronizationScope SynchScope,
1297 BasicBlock *InsertAtEnd)
1298 : Instruction(Type::getVoidTy(val->getContext()), Store,
1299 OperandTraits<StoreInst>::op_begin(this),
1300 OperandTraits<StoreInst>::operands(this),
1301 InsertAtEnd) {
1302 Op<0>() = val;
1303 Op<1>() = addr;
1304 setVolatile(isVolatile);
1305 setAlignment(Align);
1306 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001307 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001308}
1309
Christopher Lamb84485702007-04-22 19:24:39 +00001310void StoreInst::setAlignment(unsigned Align) {
1311 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001312 assert(Align <= MaximumAlignment &&
1313 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001314 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001315 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001316 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001317}
1318
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001319//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001320// AtomicCmpXchgInst Implementation
1321//===----------------------------------------------------------------------===//
1322
1323void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1324 AtomicOrdering Ordering,
1325 SynchronizationScope SynchScope) {
1326 Op<0>() = Ptr;
1327 Op<1>() = Cmp;
1328 Op<2>() = NewVal;
1329 setOrdering(Ordering);
1330 setSynchScope(SynchScope);
1331
1332 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1333 "All operands must be non-null!");
1334 assert(getOperand(0)->getType()->isPointerTy() &&
1335 "Ptr must have pointer type!");
1336 assert(getOperand(1)->getType() ==
1337 cast<PointerType>(getOperand(0)->getType())->getElementType()
1338 && "Ptr must be a pointer to Cmp type!");
1339 assert(getOperand(2)->getType() ==
1340 cast<PointerType>(getOperand(0)->getType())->getElementType()
1341 && "Ptr must be a pointer to NewVal type!");
1342 assert(Ordering != NotAtomic &&
1343 "AtomicCmpXchg instructions must be atomic!");
1344}
1345
1346AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1347 AtomicOrdering Ordering,
1348 SynchronizationScope SynchScope,
1349 Instruction *InsertBefore)
1350 : Instruction(Cmp->getType(), AtomicCmpXchg,
1351 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1352 OperandTraits<AtomicCmpXchgInst>::operands(this),
1353 InsertBefore) {
1354 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1355}
1356
1357AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1358 AtomicOrdering Ordering,
1359 SynchronizationScope SynchScope,
1360 BasicBlock *InsertAtEnd)
1361 : Instruction(Cmp->getType(), AtomicCmpXchg,
1362 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1363 OperandTraits<AtomicCmpXchgInst>::operands(this),
1364 InsertAtEnd) {
1365 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1366}
1367
1368//===----------------------------------------------------------------------===//
1369// AtomicRMWInst Implementation
1370//===----------------------------------------------------------------------===//
1371
1372void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1373 AtomicOrdering Ordering,
1374 SynchronizationScope SynchScope) {
1375 Op<0>() = Ptr;
1376 Op<1>() = Val;
1377 setOperation(Operation);
1378 setOrdering(Ordering);
1379 setSynchScope(SynchScope);
1380
1381 assert(getOperand(0) && getOperand(1) &&
1382 "All operands must be non-null!");
1383 assert(getOperand(0)->getType()->isPointerTy() &&
1384 "Ptr must have pointer type!");
1385 assert(getOperand(1)->getType() ==
1386 cast<PointerType>(getOperand(0)->getType())->getElementType()
1387 && "Ptr must be a pointer to Val type!");
1388 assert(Ordering != NotAtomic &&
1389 "AtomicRMW instructions must be atomic!");
1390}
1391
1392AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1393 AtomicOrdering Ordering,
1394 SynchronizationScope SynchScope,
1395 Instruction *InsertBefore)
1396 : Instruction(Val->getType(), AtomicRMW,
1397 OperandTraits<AtomicRMWInst>::op_begin(this),
1398 OperandTraits<AtomicRMWInst>::operands(this),
1399 InsertBefore) {
1400 Init(Operation, Ptr, Val, Ordering, SynchScope);
1401}
1402
1403AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1404 AtomicOrdering Ordering,
1405 SynchronizationScope SynchScope,
1406 BasicBlock *InsertAtEnd)
1407 : Instruction(Val->getType(), AtomicRMW,
1408 OperandTraits<AtomicRMWInst>::op_begin(this),
1409 OperandTraits<AtomicRMWInst>::operands(this),
1410 InsertAtEnd) {
1411 Init(Operation, Ptr, Val, Ordering, SynchScope);
1412}
1413
1414//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001415// FenceInst Implementation
1416//===----------------------------------------------------------------------===//
1417
1418FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1419 SynchronizationScope SynchScope,
1420 Instruction *InsertBefore)
1421 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1422 setOrdering(Ordering);
1423 setSynchScope(SynchScope);
1424}
1425
1426FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1427 SynchronizationScope SynchScope,
1428 BasicBlock *InsertAtEnd)
1429 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1430 setOrdering(Ordering);
1431 setSynchScope(SynchScope);
1432}
1433
1434//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001435// GetElementPtrInst Implementation
1436//===----------------------------------------------------------------------===//
1437
Jay Foadd1b78492011-07-25 09:48:08 +00001438void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001439 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001440 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1441 OperandList[0] = Ptr;
1442 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001443 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001444}
1445
Gabor Greiff6caff662008-05-10 08:32:32 +00001446GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001447 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001448 OperandTraits<GetElementPtrInst>::op_end(this)
1449 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001450 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001451 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001452 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001453}
1454
Chris Lattner6090a422009-03-09 04:46:40 +00001455/// getIndexedType - Returns the type of the element that would be accessed with
1456/// a gep instruction with the specified parameters.
1457///
1458/// The Idxs pointer should point to a continuous piece of memory containing the
1459/// indices, either as Value* or uint64_t.
1460///
1461/// A null type is returned if the indices are invalid for the specified
1462/// pointer type.
1463///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001464template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001465static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001466 if (Ptr->isVectorTy()) {
1467 assert(IdxList.size() == 1 &&
1468 "GEP with vector pointers must have a single index");
1469 PointerType *PTy = dyn_cast<PointerType>(
1470 cast<VectorType>(Ptr)->getElementType());
1471 assert(PTy && "Gep with invalid vector pointer found");
1472 return PTy->getElementType();
1473 }
1474
Chris Lattner229907c2011-07-18 04:54:35 +00001475 PointerType *PTy = dyn_cast<PointerType>(Ptr);
Dan Gohman12fce772008-05-15 19:50:34 +00001476 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001477 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001478
Chris Lattner6090a422009-03-09 04:46:40 +00001479 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001480 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001481 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001482
Chris Lattner6090a422009-03-09 04:46:40 +00001483 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001484 // it cannot be 'stepped over'.
1485 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001486 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001487
Dan Gohman1ecaf452008-05-31 00:58:22 +00001488 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001489 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001490 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001491 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001492 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001493 if (!CT->indexValid(Index)) return 0;
1494 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001495 }
Jay Foadd1b78492011-07-25 09:48:08 +00001496 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001497}
1498
Jay Foadd1b78492011-07-25 09:48:08 +00001499Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1500 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001501}
1502
Chris Lattner229907c2011-07-18 04:54:35 +00001503Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001504 ArrayRef<Constant *> IdxList) {
1505 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001506}
1507
Jay Foadd1b78492011-07-25 09:48:08 +00001508Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1509 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001510}
1511
Nadav Rotem3924cb02011-12-05 06:29:09 +00001512unsigned GetElementPtrInst::getAddressSpace(Value *Ptr) {
1513 Type *Ty = Ptr->getType();
1514
1515 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1516 Ty = VTy->getElementType();
1517
1518 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
1519 return PTy->getAddressSpace();
1520
Craig Topperc514b542012-02-05 22:14:15 +00001521 llvm_unreachable("Invalid GEP pointer type");
Nadav Rotem3924cb02011-12-05 06:29:09 +00001522}
1523
Chris Lattner45f15572007-04-14 00:12:57 +00001524/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1525/// zeros. If so, the result pointer and the first operand have the same
1526/// value, just potentially different types.
1527bool GetElementPtrInst::hasAllZeroIndices() const {
1528 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1529 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1530 if (!CI->isZero()) return false;
1531 } else {
1532 return false;
1533 }
1534 }
1535 return true;
1536}
1537
Chris Lattner27058292007-04-27 20:35:56 +00001538/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1539/// constant integers. If so, the result pointer and the first operand have
1540/// a constant offset between them.
1541bool GetElementPtrInst::hasAllConstantIndices() const {
1542 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1543 if (!isa<ConstantInt>(getOperand(i)))
1544 return false;
1545 }
1546 return true;
1547}
1548
Dan Gohman1b849082009-09-07 23:54:19 +00001549void GetElementPtrInst::setIsInBounds(bool B) {
1550 cast<GEPOperator>(this)->setIsInBounds(B);
1551}
Chris Lattner45f15572007-04-14 00:12:57 +00001552
Nick Lewycky28a5f252009-09-27 21:33:04 +00001553bool GetElementPtrInst::isInBounds() const {
1554 return cast<GEPOperator>(this)->isInBounds();
1555}
1556
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001557//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001558// ExtractElementInst Implementation
1559//===----------------------------------------------------------------------===//
1560
1561ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001562 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001563 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001564 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001565 ExtractElement,
1566 OperandTraits<ExtractElementInst>::op_begin(this),
1567 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001568 assert(isValidOperands(Val, Index) &&
1569 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001570 Op<0>() = Val;
1571 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001572 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001573}
1574
1575ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001576 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001577 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001578 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001579 ExtractElement,
1580 OperandTraits<ExtractElementInst>::op_begin(this),
1581 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001582 assert(isValidOperands(Val, Index) &&
1583 "Invalid extractelement instruction operands!");
1584
Gabor Greif2d3024d2008-05-26 21:33:52 +00001585 Op<0>() = Val;
1586 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001587 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001588}
1589
Chris Lattner65511ff2006-10-05 06:24:58 +00001590
Chris Lattner54865b32006-04-08 04:05:48 +00001591bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001592 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001593 return false;
1594 return true;
1595}
1596
1597
Robert Bocchino23004482006-01-10 19:05:34 +00001598//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001599// InsertElementInst Implementation
1600//===----------------------------------------------------------------------===//
1601
Chris Lattner54865b32006-04-08 04:05:48 +00001602InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001603 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001604 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001605 : Instruction(Vec->getType(), InsertElement,
1606 OperandTraits<InsertElementInst>::op_begin(this),
1607 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001608 assert(isValidOperands(Vec, Elt, Index) &&
1609 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001610 Op<0>() = Vec;
1611 Op<1>() = Elt;
1612 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001613 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001614}
1615
Chris Lattner54865b32006-04-08 04:05:48 +00001616InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001617 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001618 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001619 : Instruction(Vec->getType(), InsertElement,
1620 OperandTraits<InsertElementInst>::op_begin(this),
1621 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001622 assert(isValidOperands(Vec, Elt, Index) &&
1623 "Invalid insertelement instruction operands!");
1624
Gabor Greif2d3024d2008-05-26 21:33:52 +00001625 Op<0>() = Vec;
1626 Op<1>() = Elt;
1627 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001628 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001629}
1630
Chris Lattner54865b32006-04-08 04:05:48 +00001631bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1632 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001633 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001634 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001635
Reid Spencerd84d35b2007-02-15 02:26:10 +00001636 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001637 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001638
Duncan Sands9dff9be2010-02-15 16:12:20 +00001639 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001640 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001641 return true;
1642}
1643
1644
Robert Bocchinoca27f032006-01-17 20:07:22 +00001645//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001646// ShuffleVectorInst Implementation
1647//===----------------------------------------------------------------------===//
1648
1649ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001650 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001651 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001652: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001653 cast<VectorType>(Mask->getType())->getNumElements()),
1654 ShuffleVector,
1655 OperandTraits<ShuffleVectorInst>::op_begin(this),
1656 OperandTraits<ShuffleVectorInst>::operands(this),
1657 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001658 assert(isValidOperands(V1, V2, Mask) &&
1659 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001660 Op<0>() = V1;
1661 Op<1>() = V2;
1662 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001663 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001664}
1665
1666ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001667 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001668 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001669: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1670 cast<VectorType>(Mask->getType())->getNumElements()),
1671 ShuffleVector,
1672 OperandTraits<ShuffleVectorInst>::op_begin(this),
1673 OperandTraits<ShuffleVectorInst>::operands(this),
1674 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001675 assert(isValidOperands(V1, V2, Mask) &&
1676 "Invalid shuffle vector instruction operands!");
1677
Gabor Greif2d3024d2008-05-26 21:33:52 +00001678 Op<0>() = V1;
1679 Op<1>() = V2;
1680 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001681 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001682}
1683
Mon P Wang25f01062008-11-10 04:46:22 +00001684bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001685 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001686 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001687 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001688 return false;
1689
Chris Lattner1dcb6542012-01-25 23:49:49 +00001690 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001691 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001692 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001693 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001694
1695 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001696 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1697 return true;
1698
Nate Begeman60a31c32010-08-13 00:16:46 +00001699 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001700 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001701 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001702 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1703 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001704 return false;
1705 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1706 return false;
1707 }
1708 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001709 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001710 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001711
1712 if (const ConstantDataSequential *CDS =
1713 dyn_cast<ConstantDataSequential>(Mask)) {
1714 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1715 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1716 if (CDS->getElementAsInteger(i) >= V1Size*2)
1717 return false;
1718 return true;
1719 }
1720
1721 // The bitcode reader can create a place holder for a forward reference
1722 // used as the shuffle mask. When this occurs, the shuffle mask will
1723 // fall into this case and fail. To avoid this error, do this bit of
1724 // ugliness to allow such a mask pass.
1725 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1726 if (CE->getOpcode() == Instruction::UserOp1)
1727 return true;
1728
1729 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001730}
1731
Chris Lattnerf724e342008-03-02 05:28:33 +00001732/// getMaskValue - Return the index from the shuffle mask for the specified
1733/// output result. This is either -1 if the element is undef or a number less
1734/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001735int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1736 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1737 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001738 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001739 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001740 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001741 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001742 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001743}
1744
Chris Lattner1dcb6542012-01-25 23:49:49 +00001745/// getShuffleMask - Return the full mask for this instruction, where each
1746/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001747void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1748 SmallVectorImpl<int> &Result) {
1749 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001750
Chris Lattnercf129702012-01-26 02:51:13 +00001751 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001752 for (unsigned i = 0; i != NumElts; ++i)
1753 Result.push_back(CDS->getElementAsInteger(i));
1754 return;
1755 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001756 for (unsigned i = 0; i != NumElts; ++i) {
1757 Constant *C = Mask->getAggregateElement(i);
1758 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001759 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001760 }
1761}
1762
1763
Dan Gohman12fce772008-05-15 19:50:34 +00001764//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001765// InsertValueInst Class
1766//===----------------------------------------------------------------------===//
1767
Jay Foad57aa6362011-07-13 10:26:04 +00001768void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1769 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001770 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001771
1772 // There's no fundamental reason why we require at least one index
1773 // (other than weirdness with &*IdxBegin being invalid; see
1774 // getelementptr's init routine for example). But there's no
1775 // present need to support it.
1776 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1777
1778 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001779 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001780 Op<0>() = Agg;
1781 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001782
Jay Foad57aa6362011-07-13 10:26:04 +00001783 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001784 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001785}
1786
1787InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001788 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001789 OperandTraits<InsertValueInst>::op_begin(this), 2),
1790 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001791 Op<0>() = IVI.getOperand(0);
1792 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001793 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001794}
1795
1796//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001797// ExtractValueInst Class
1798//===----------------------------------------------------------------------===//
1799
Jay Foad57aa6362011-07-13 10:26:04 +00001800void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001801 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001802
Jay Foad57aa6362011-07-13 10:26:04 +00001803 // There's no fundamental reason why we require at least one index.
1804 // But there's no present need to support it.
1805 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001806
Jay Foad57aa6362011-07-13 10:26:04 +00001807 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001808 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001809}
1810
1811ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001812 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001813 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001814 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001815}
1816
Dan Gohman12fce772008-05-15 19:50:34 +00001817// getIndexedType - Returns the type of the element that would be extracted
1818// with an extractvalue instruction with the specified parameters.
1819//
1820// A null type is returned if the indices are invalid for the specified
1821// pointer type.
1822//
Chris Lattner229907c2011-07-18 04:54:35 +00001823Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001824 ArrayRef<unsigned> Idxs) {
1825 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001826 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001827 // We can't use CompositeType::indexValid(Index) here.
1828 // indexValid() always returns true for arrays because getelementptr allows
1829 // out-of-bounds indices. Since we don't allow those for extractvalue and
1830 // insertvalue we need to check array indexing manually.
1831 // Since the only other types we can index into are struct types it's just
1832 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001833 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001834 if (Index >= AT->getNumElements())
1835 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001836 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001837 if (Index >= ST->getNumElements())
1838 return 0;
1839 } else {
1840 // Not a valid type to index into.
1841 return 0;
1842 }
1843
1844 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001845 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001846 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001847}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001848
1849//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001850// BinaryOperator Class
1851//===----------------------------------------------------------------------===//
1852
Chris Lattner2195fc42007-02-24 00:55:48 +00001853BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001854 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001855 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001856 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001857 OperandTraits<BinaryOperator>::op_begin(this),
1858 OperandTraits<BinaryOperator>::operands(this),
1859 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001860 Op<0>() = S1;
1861 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001862 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001863 setName(Name);
1864}
1865
1866BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001867 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001868 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001869 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001870 OperandTraits<BinaryOperator>::op_begin(this),
1871 OperandTraits<BinaryOperator>::operands(this),
1872 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001873 Op<0>() = S1;
1874 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001875 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001876 setName(Name);
1877}
1878
1879
1880void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001881 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001882 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001883 assert(LHS->getType() == RHS->getType() &&
1884 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001885#ifndef NDEBUG
1886 switch (iType) {
1887 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001888 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001889 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001890 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001891 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001892 "Tried to create an integer operation on a non-integer type!");
1893 break;
1894 case FAdd: case FSub:
1895 case FMul:
1896 assert(getType() == LHS->getType() &&
1897 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001898 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001899 "Tried to create a floating-point operation on a "
1900 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001901 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001902 case UDiv:
1903 case SDiv:
1904 assert(getType() == LHS->getType() &&
1905 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001906 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001907 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001908 "Incorrect operand type (not integer) for S/UDIV");
1909 break;
1910 case FDiv:
1911 assert(getType() == LHS->getType() &&
1912 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001913 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001914 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001915 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001916 case URem:
1917 case SRem:
1918 assert(getType() == LHS->getType() &&
1919 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001920 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001921 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001922 "Incorrect operand type (not integer) for S/UREM");
1923 break;
1924 case FRem:
1925 assert(getType() == LHS->getType() &&
1926 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001927 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001928 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001929 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001930 case Shl:
1931 case LShr:
1932 case AShr:
1933 assert(getType() == LHS->getType() &&
1934 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001935 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001936 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001937 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001938 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001939 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001940 case And: case Or:
1941 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001942 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001943 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001944 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001945 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001946 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001947 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001948 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001949 default:
1950 break;
1951 }
1952#endif
1953}
1954
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001955BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001956 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001957 Instruction *InsertBefore) {
1958 assert(S1->getType() == S2->getType() &&
1959 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001960 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001961}
1962
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001963BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001964 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001965 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001966 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001967 InsertAtEnd->getInstList().push_back(Res);
1968 return Res;
1969}
1970
Dan Gohman5476cfd2009-08-12 16:23:25 +00001971BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001972 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001973 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001974 return new BinaryOperator(Instruction::Sub,
1975 zero, Op,
1976 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001977}
1978
Dan Gohman5476cfd2009-08-12 16:23:25 +00001979BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001980 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001981 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001982 return new BinaryOperator(Instruction::Sub,
1983 zero, Op,
1984 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001985}
1986
Dan Gohman4ab44202009-12-18 02:58:50 +00001987BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1988 Instruction *InsertBefore) {
1989 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1990 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1991}
1992
1993BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1994 BasicBlock *InsertAtEnd) {
1995 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1996 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1997}
1998
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001999BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2000 Instruction *InsertBefore) {
2001 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2002 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2003}
2004
2005BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2006 BasicBlock *InsertAtEnd) {
2007 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2008 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2009}
2010
Dan Gohman5476cfd2009-08-12 16:23:25 +00002011BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002012 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002013 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002014 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002015 Op->getType(), Name, InsertBefore);
2016}
2017
Dan Gohman5476cfd2009-08-12 16:23:25 +00002018BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002019 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002020 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002021 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002022 Op->getType(), Name, InsertAtEnd);
2023}
2024
Dan Gohman5476cfd2009-08-12 16:23:25 +00002025BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002026 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002027 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002028 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002029 Op->getType(), Name, InsertBefore);
2030}
2031
Dan Gohman5476cfd2009-08-12 16:23:25 +00002032BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002033 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002034 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002035 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002036 Op->getType(), Name, InsertAtEnd);
2037}
2038
2039
2040// isConstantAllOnes - Helper function for several functions below
2041static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002042 if (const Constant *C = dyn_cast<Constant>(V))
2043 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002044 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002045}
2046
Owen Andersonbb2501b2009-07-13 22:18:28 +00002047bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002048 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2049 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002050 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2051 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002052 return false;
2053}
2054
Owen Andersonbb2501b2009-07-13 22:18:28 +00002055bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002056 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2057 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002058 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00002059 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00002060 return false;
2061}
2062
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002063bool BinaryOperator::isNot(const Value *V) {
2064 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2065 return (Bop->getOpcode() == Instruction::Xor &&
2066 (isConstantAllOnes(Bop->getOperand(1)) ||
2067 isConstantAllOnes(Bop->getOperand(0))));
2068 return false;
2069}
2070
Chris Lattner2c7d1772005-04-24 07:28:37 +00002071Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002072 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002073}
2074
Chris Lattner2c7d1772005-04-24 07:28:37 +00002075const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2076 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002077}
2078
Dan Gohmana5b96452009-06-04 22:49:04 +00002079Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002080 return cast<BinaryOperator>(BinOp)->getOperand(1);
2081}
2082
2083const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2084 return getFNegArgument(const_cast<Value*>(BinOp));
2085}
2086
Chris Lattner2c7d1772005-04-24 07:28:37 +00002087Value *BinaryOperator::getNotArgument(Value *BinOp) {
2088 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2089 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2090 Value *Op0 = BO->getOperand(0);
2091 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002092 if (isConstantAllOnes(Op0)) return Op1;
2093
2094 assert(isConstantAllOnes(Op1));
2095 return Op0;
2096}
2097
Chris Lattner2c7d1772005-04-24 07:28:37 +00002098const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2099 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002100}
2101
2102
2103// swapOperands - Exchange the two operands to this instruction. This
2104// instruction is safe to use on any binary instruction and does not
2105// modify the semantics of the instruction. If the instruction is
2106// order dependent (SetLT f.e.) the opcode is changed.
2107//
2108bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002109 if (!isCommutative())
2110 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002111 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002112 return false;
2113}
2114
Dan Gohman1b849082009-09-07 23:54:19 +00002115void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2116 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2117}
2118
2119void BinaryOperator::setHasNoSignedWrap(bool b) {
2120 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2121}
2122
2123void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002124 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002125}
2126
Nick Lewycky28a5f252009-09-27 21:33:04 +00002127bool BinaryOperator::hasNoUnsignedWrap() const {
2128 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2129}
2130
2131bool BinaryOperator::hasNoSignedWrap() const {
2132 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2133}
2134
2135bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002136 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002137}
2138
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002139//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002140// FPMathOperator Class
2141//===----------------------------------------------------------------------===//
2142
2143/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2144/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002145/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002146float FPMathOperator::getFPAccuracy() const {
2147 const MDNode *MD =
2148 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2149 if (!MD)
2150 return 0.0;
Duncan Sands9af62982012-04-16 19:39:33 +00002151 ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2152 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002153}
2154
2155
2156//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002157// CastInst Class
2158//===----------------------------------------------------------------------===//
2159
David Blaikie3a15e142011-12-01 08:00:17 +00002160void CastInst::anchor() {}
2161
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002162// Just determine if this cast only deals with integral->integral conversion.
2163bool CastInst::isIntegerCast() const {
2164 switch (getOpcode()) {
2165 default: return false;
2166 case Instruction::ZExt:
2167 case Instruction::SExt:
2168 case Instruction::Trunc:
2169 return true;
2170 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002171 return getOperand(0)->getType()->isIntegerTy() &&
2172 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002173 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002174}
2175
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176bool CastInst::isLosslessCast() const {
2177 // Only BitCast can be lossless, exit fast if we're not BitCast
2178 if (getOpcode() != Instruction::BitCast)
2179 return false;
2180
2181 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002182 Type* SrcTy = getOperand(0)->getType();
2183 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002184 if (SrcTy == DstTy)
2185 return true;
2186
Reid Spencer8d9336d2006-12-31 05:26:44 +00002187 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002188 if (SrcTy->isPointerTy())
2189 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190 return false; // Other types have no identity values
2191}
2192
2193/// This function determines if the CastInst does not require any bits to be
2194/// changed in order to effect the cast. Essentially, it identifies cases where
2195/// no code gen is necessary for the cast, hence the name no-op cast. For
2196/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002197/// # bitcast i32* %x to i8*
2198/// # bitcast <2 x i32> %x to <4 x i16>
2199/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002200/// @brief Determine if the described cast is a no-op.
2201bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002202 Type *SrcTy,
2203 Type *DestTy,
2204 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002205 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002206 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002207 case Instruction::Trunc:
2208 case Instruction::ZExt:
2209 case Instruction::SExt:
2210 case Instruction::FPTrunc:
2211 case Instruction::FPExt:
2212 case Instruction::UIToFP:
2213 case Instruction::SIToFP:
2214 case Instruction::FPToUI:
2215 case Instruction::FPToSI:
2216 return false; // These always modify bits
2217 case Instruction::BitCast:
2218 return true; // BitCast never modifies bits.
2219 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002220 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002221 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002223 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002224 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002225 }
2226}
2227
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002228/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002229bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002230 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2231}
2232
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002233/// This function determines if a pair of casts can be eliminated and what
2234/// opcode should be used in the elimination. This assumes that there are two
2235/// instructions like this:
2236/// * %F = firstOpcode SrcTy %x to MidTy
2237/// * %S = secondOpcode MidTy %F to DstTy
2238/// The function returns a resultOpcode so these two casts can be replaced with:
2239/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2240/// If no such cast is permited, the function returns 0.
2241unsigned CastInst::isEliminableCastPair(
2242 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002243 Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002244 // Define the 144 possibilities for these two cast instructions. The values
2245 // in this matrix determine what to do in a given situation and select the
2246 // case in the switch below. The rows correspond to firstOp, the columns
2247 // correspond to secondOp. In looking at the table below, keep in mind
2248 // the following cast properties:
2249 //
2250 // Size Compare Source Destination
2251 // Operator Src ? Size Type Sign Type Sign
2252 // -------- ------------ ------------------- ---------------------
2253 // TRUNC > Integer Any Integral Any
2254 // ZEXT < Integral Unsigned Integer Any
2255 // SEXT < Integral Signed Integer Any
2256 // FPTOUI n/a FloatPt n/a Integral Unsigned
2257 // FPTOSI n/a FloatPt n/a Integral Signed
2258 // UITOFP n/a Integral Unsigned FloatPt n/a
2259 // SITOFP n/a Integral Signed FloatPt n/a
2260 // FPTRUNC > FloatPt n/a FloatPt n/a
2261 // FPEXT < FloatPt n/a FloatPt n/a
2262 // PTRTOINT n/a Pointer n/a Integral Unsigned
2263 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002264 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002265 //
2266 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002267 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2268 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002269 // of the produced value (we no longer know the top-part is all zeros).
2270 // Further this conversion is often much more expensive for typical hardware,
2271 // and causes issues when building libgcc. We disallow fptosi+sext for the
2272 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002273 const unsigned numCastOps =
2274 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2275 static const uint8_t CastResults[numCastOps][numCastOps] = {
2276 // T F F U S F F P I B -+
2277 // R Z S P P I I T P 2 N T |
2278 // U E E 2 2 2 2 R E I T C +- secondOp
2279 // N X X U S F F N X N 2 V |
2280 // C T T I I P P C T T P T -+
2281 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2282 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2283 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002284 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2285 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002286 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2287 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2288 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2289 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2290 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2291 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2292 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2293 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002294
2295 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002296 // merging. However, bitcast of A->B->A are allowed.
2297 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2298 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2299 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2300
2301 // Check if any of the bitcasts convert scalars<->vectors.
2302 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2303 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2304 // Unless we are bitcasing to the original type, disallow optimizations.
2305 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306
2307 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2308 [secondOp-Instruction::CastOpsBegin];
2309 switch (ElimCase) {
2310 case 0:
2311 // categorically disallowed
2312 return 0;
2313 case 1:
2314 // allowed, use first cast's opcode
2315 return firstOp;
2316 case 2:
2317 // allowed, use second cast's opcode
2318 return secondOp;
2319 case 3:
2320 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002321 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002322 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002323 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 return firstOp;
2325 return 0;
2326 case 4:
2327 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002328 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002329 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330 return firstOp;
2331 return 0;
2332 case 5:
2333 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002334 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002335 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002336 return secondOp;
2337 return 0;
2338 case 6:
2339 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002340 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002341 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342 return secondOp;
2343 return 0;
2344 case 7: {
2345 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002346 if (!IntPtrTy)
2347 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002348 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2349 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002350 if (MidSize >= PtrSize)
2351 return Instruction::BitCast;
2352 return 0;
2353 }
2354 case 8: {
2355 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2356 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2357 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002358 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2359 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360 if (SrcSize == DstSize)
2361 return Instruction::BitCast;
2362 else if (SrcSize < DstSize)
2363 return firstOp;
2364 return secondOp;
2365 }
2366 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2367 return Instruction::ZExt;
2368 case 10:
2369 // fpext followed by ftrunc is allowed if the bit size returned to is
2370 // the same as the original, in which case its just a bitcast
2371 if (SrcTy == DstTy)
2372 return Instruction::BitCast;
2373 return 0; // If the types are not the same we can't eliminate it.
2374 case 11:
2375 // bitcast followed by ptrtoint is allowed as long as the bitcast
2376 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002377 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 return secondOp;
2379 return 0;
2380 case 12:
2381 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002382 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002383 return firstOp;
2384 return 0;
2385 case 13: {
2386 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002387 if (!IntPtrTy)
2388 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002389 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2390 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2391 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392 if (SrcSize <= PtrSize && SrcSize == DstSize)
2393 return Instruction::BitCast;
2394 return 0;
2395 }
2396 case 99:
2397 // cast combination can't happen (error in input). This is for all cases
2398 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002399 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400 default:
Craig Topperc514b542012-02-05 22:14:15 +00002401 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403}
2404
Chris Lattner229907c2011-07-18 04:54:35 +00002405CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002406 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002407 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408 // Construct and return the appropriate CastInst subclass
2409 switch (op) {
2410 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2411 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2412 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2413 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2414 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2415 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2416 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2417 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2418 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2419 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2420 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2421 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topperc514b542012-02-05 22:14:15 +00002422 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424}
2425
Chris Lattner229907c2011-07-18 04:54:35 +00002426CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002427 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002428 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429 // Construct and return the appropriate CastInst subclass
2430 switch (op) {
2431 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2432 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2433 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2434 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2435 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2436 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2437 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2438 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2439 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2440 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2441 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2442 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topperc514b542012-02-05 22:14:15 +00002443 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002445}
2446
Chris Lattner229907c2011-07-18 04:54:35 +00002447CastInst *CastInst::CreateZExtOrBitCast(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::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002453}
2454
Chris Lattner229907c2011-07-18 04:54:35 +00002455CastInst *CastInst::CreateZExtOrBitCast(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::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002461}
2462
Chris Lattner229907c2011-07-18 04:54:35 +00002463CastInst *CastInst::CreateSExtOrBitCast(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::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002469}
2470
Chris Lattner229907c2011-07-18 04:54:35 +00002471CastInst *CastInst::CreateSExtOrBitCast(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::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002477}
2478
Chris Lattner229907c2011-07-18 04:54:35 +00002479CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002480 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002481 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002482 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002483 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2484 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002485}
2486
Chris Lattner229907c2011-07-18 04:54:35 +00002487CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002488 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002489 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002490 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002491 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2492 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002493}
2494
Chris Lattner229907c2011-07-18 04:54:35 +00002495CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002496 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002497 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002498 assert(S->getType()->isPointerTy() && "Invalid cast");
2499 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002500 "Invalid cast");
2501
Duncan Sands9dff9be2010-02-15 16:12:20 +00002502 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002503 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2504 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002505}
2506
2507/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002508CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002509 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002510 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002511 assert(S->getType()->isPointerTy() && "Invalid cast");
2512 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002513 "Invalid cast");
2514
Duncan Sands9dff9be2010-02-15 16:12:20 +00002515 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002516 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2517 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002518}
2519
Chris Lattner229907c2011-07-18 04:54:35 +00002520CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002521 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002522 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002523 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002524 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002525 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2526 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002527 Instruction::CastOps opcode =
2528 (SrcBits == DstBits ? Instruction::BitCast :
2529 (SrcBits > DstBits ? Instruction::Trunc :
2530 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002531 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002532}
2533
Chris Lattner229907c2011-07-18 04:54:35 +00002534CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002535 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002536 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002537 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002538 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002539 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2540 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002541 Instruction::CastOps opcode =
2542 (SrcBits == DstBits ? Instruction::BitCast :
2543 (SrcBits > DstBits ? Instruction::Trunc :
2544 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002545 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002546}
2547
Chris Lattner229907c2011-07-18 04:54:35 +00002548CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002549 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002550 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002551 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002552 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002553 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2554 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002555 Instruction::CastOps opcode =
2556 (SrcBits == DstBits ? Instruction::BitCast :
2557 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002558 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002559}
2560
Chris Lattner229907c2011-07-18 04:54:35 +00002561CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002562 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002563 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002564 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002565 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002566 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2567 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002568 Instruction::CastOps opcode =
2569 (SrcBits == DstBits ? Instruction::BitCast :
2570 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002571 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002572}
2573
Duncan Sands55e50902008-01-06 10:12:28 +00002574// Check whether it is valid to call getCastOpcode for these types.
2575// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002576bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002577 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2578 return false;
2579
2580 if (SrcTy == DestTy)
2581 return true;
2582
Chris Lattner229907c2011-07-18 04:54:35 +00002583 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2584 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002585 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2586 // An element by element cast. Valid if casting the elements is valid.
2587 SrcTy = SrcVecTy->getElementType();
2588 DestTy = DestVecTy->getElementType();
2589 }
2590
Duncan Sands55e50902008-01-06 10:12:28 +00002591 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002592 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2593 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002594
2595 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002596 if (DestTy->isIntegerTy()) { // Casting to integral
2597 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002598 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002599 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002600 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002601 } else if (SrcTy->isVectorTy()) { // Casting from vector
2602 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002603 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002604 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002605 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002606 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2607 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002608 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002609 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002610 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002611 } else if (SrcTy->isVectorTy()) { // Casting from vector
2612 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002613 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002614 return false;
2615 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002616 } else if (DestTy->isVectorTy()) { // Casting to vector
2617 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002618 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002619 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002620 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002621 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002622 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002623 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002624 return false;
2625 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002626 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002627 if (SrcTy->isVectorTy()) {
2628 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002629 } else {
2630 return false;
2631 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002632 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002633 return false;
2634 }
2635}
2636
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637// Provide a way to get a "cast" where the cast opcode is inferred from the
2638// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002639// logic in the castIsValid function below. This axiom should hold:
2640// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2641// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002643// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002644Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002645CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002646 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2647 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648
Duncan Sands55e50902008-01-06 10:12:28 +00002649 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2650 "Only first class types are castable!");
2651
Duncan Sandsa8514532011-05-18 07:13:41 +00002652 if (SrcTy == DestTy)
2653 return BitCast;
2654
Chris Lattner229907c2011-07-18 04:54:35 +00002655 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2656 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002657 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2658 // An element by element cast. Find the appropriate opcode based on the
2659 // element types.
2660 SrcTy = SrcVecTy->getElementType();
2661 DestTy = DestVecTy->getElementType();
2662 }
2663
2664 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002665 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2666 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002667
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002668 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002669 if (DestTy->isIntegerTy()) { // Casting to integral
2670 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671 if (DestBits < SrcBits)
2672 return Trunc; // int -> smaller int
2673 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002674 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002675 return SExt; // signed -> SEXT
2676 else
2677 return ZExt; // unsigned -> ZEXT
2678 } else {
2679 return BitCast; // Same size, No-op cast
2680 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002681 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002682 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002683 return FPToSI; // FP -> sint
2684 else
2685 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002686 } else if (SrcTy->isVectorTy()) {
2687 assert(DestBits == SrcBits &&
2688 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002689 return BitCast; // Same size, no-op cast
2690 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002691 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002692 "Casting from a value that is not first-class type");
2693 return PtrToInt; // ptr -> int
2694 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002695 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2696 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002697 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002698 return SIToFP; // sint -> FP
2699 else
2700 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002701 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002702 if (DestBits < SrcBits) {
2703 return FPTrunc; // FP -> smaller FP
2704 } else if (DestBits > SrcBits) {
2705 return FPExt; // FP -> larger FP
2706 } else {
2707 return BitCast; // same size, no-op cast
2708 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002709 } else if (SrcTy->isVectorTy()) {
2710 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002711 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002712 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002713 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002714 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002715 } else if (DestTy->isVectorTy()) {
2716 assert(DestBits == SrcBits &&
2717 "Illegal cast to vector (wrong type or size)");
2718 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002719 } else if (DestTy->isPointerTy()) {
2720 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002721 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002722 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002723 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002724 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002725 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002726 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002727 if (SrcTy->isVectorTy()) {
2728 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002729 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002730 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002731 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002732 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002733 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002734}
2735
2736//===----------------------------------------------------------------------===//
2737// CastInst SubClass Constructors
2738//===----------------------------------------------------------------------===//
2739
2740/// Check that the construction parameters for a CastInst are correct. This
2741/// could be broken out into the separate constructors but it is useful to have
2742/// it in one place and to eliminate the redundant code for getting the sizes
2743/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002744bool
Chris Lattner229907c2011-07-18 04:54:35 +00002745CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002746
2747 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002748 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002749 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2750 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002751 return false;
2752
2753 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002754 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2755 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002756
Duncan Sands7f646562011-05-18 09:21:57 +00002757 // If these are vector types, get the lengths of the vectors (using zero for
2758 // scalar types means that checking that vector lengths match also checks that
2759 // scalars are not being converted to vectors or vectors to scalars).
2760 unsigned SrcLength = SrcTy->isVectorTy() ?
2761 cast<VectorType>(SrcTy)->getNumElements() : 0;
2762 unsigned DstLength = DstTy->isVectorTy() ?
2763 cast<VectorType>(DstTy)->getNumElements() : 0;
2764
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002765 // Switch on the opcode provided
2766 switch (op) {
2767 default: return false; // This is an input error
2768 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002769 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2770 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002771 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002772 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2773 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002774 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002775 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2776 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002777 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002778 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2779 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002780 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002781 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2782 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002783 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002784 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002785 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2786 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002787 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002788 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002789 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2790 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002791 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002792 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002793 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002794 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2795 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2796 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002797 return SrcTy->getScalarType()->isPointerTy() &&
2798 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002799 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002800 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002801 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002802 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2803 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2804 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002805 return SrcTy->getScalarType()->isIntegerTy() &&
2806 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002807 case Instruction::BitCast:
2808 // BitCast implies a no-op cast of type only. No bits change.
2809 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002810 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002811 return false;
2812
Duncan Sands55e50902008-01-06 10:12:28 +00002813 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002814 // these cases, the cast is okay if the source and destination bit widths
2815 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002816 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002817 }
2818}
2819
2820TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002821 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002822) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002823 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002824}
2825
2826TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002827 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002828) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002829 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002830}
2831
2832ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002833 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002834) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002835 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002836}
2837
2838ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002839 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002840) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002841 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002842}
2843SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002844 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002845) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002846 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002847}
2848
Jeff Cohencc08c832006-12-02 02:22:01 +00002849SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002850 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002851) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002852 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002853}
2854
2855FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002856 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002857) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002858 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002859}
2860
2861FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002862 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002863) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002864 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002865}
2866
2867FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002868 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002869) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002870 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002871}
2872
2873FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002874 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002875) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002876 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002877}
2878
2879UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002880 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002881) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002882 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002883}
2884
2885UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002886 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002887) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002888 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002889}
2890
2891SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002892 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002893) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002894 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002895}
2896
2897SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002898 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002899) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002900 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002901}
2902
2903FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002904 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002905) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002906 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002907}
2908
2909FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002910 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002911) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002912 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002913}
2914
2915FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002916 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002917) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002918 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002919}
2920
2921FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002922 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002923) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002924 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002925}
2926
2927PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002928 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002929) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002930 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002931}
2932
2933PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002934 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002935) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002936 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002937}
2938
2939IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002940 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002942 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002943}
2944
2945IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002946 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002947) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002948 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949}
2950
2951BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002952 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002953) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002954 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002955}
2956
2957BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002958 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002959) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002960 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002961}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002962
2963//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002964// CmpInst Classes
2965//===----------------------------------------------------------------------===//
2966
Craig Topper3186c012012-09-23 02:12:10 +00002967void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002968
Chris Lattner229907c2011-07-18 04:54:35 +00002969CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002970 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002971 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002972 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002973 OperandTraits<CmpInst>::op_begin(this),
2974 OperandTraits<CmpInst>::operands(this),
2975 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002976 Op<0>() = LHS;
2977 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002978 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002979 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002980}
Gabor Greiff6caff662008-05-10 08:32:32 +00002981
Chris Lattner229907c2011-07-18 04:54:35 +00002982CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002983 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002984 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002985 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002986 OperandTraits<CmpInst>::op_begin(this),
2987 OperandTraits<CmpInst>::operands(this),
2988 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002989 Op<0>() = LHS;
2990 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002991 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002992 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002993}
2994
2995CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002996CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002997 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002998 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002999 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003000 if (InsertBefore)
3001 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3002 S1, S2, Name);
3003 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003004 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003005 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003006 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003007
3008 if (InsertBefore)
3009 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3010 S1, S2, Name);
3011 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003012 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003013 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003014}
3015
3016CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003017CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003018 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003019 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003020 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3021 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003022 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003023 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3024 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003025}
3026
3027void CmpInst::swapOperands() {
3028 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3029 IC->swapOperands();
3030 else
3031 cast<FCmpInst>(this)->swapOperands();
3032}
3033
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003034bool CmpInst::isCommutative() const {
3035 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003036 return IC->isCommutative();
3037 return cast<FCmpInst>(this)->isCommutative();
3038}
3039
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003040bool CmpInst::isEquality() const {
3041 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003042 return IC->isEquality();
3043 return cast<FCmpInst>(this)->isEquality();
3044}
3045
3046
Dan Gohman4e724382008-05-31 02:47:54 +00003047CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003048 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003049 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003050 case ICMP_EQ: return ICMP_NE;
3051 case ICMP_NE: return ICMP_EQ;
3052 case ICMP_UGT: return ICMP_ULE;
3053 case ICMP_ULT: return ICMP_UGE;
3054 case ICMP_UGE: return ICMP_ULT;
3055 case ICMP_ULE: return ICMP_UGT;
3056 case ICMP_SGT: return ICMP_SLE;
3057 case ICMP_SLT: return ICMP_SGE;
3058 case ICMP_SGE: return ICMP_SLT;
3059 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003060
Dan Gohman4e724382008-05-31 02:47:54 +00003061 case FCMP_OEQ: return FCMP_UNE;
3062 case FCMP_ONE: return FCMP_UEQ;
3063 case FCMP_OGT: return FCMP_ULE;
3064 case FCMP_OLT: return FCMP_UGE;
3065 case FCMP_OGE: return FCMP_ULT;
3066 case FCMP_OLE: return FCMP_UGT;
3067 case FCMP_UEQ: return FCMP_ONE;
3068 case FCMP_UNE: return FCMP_OEQ;
3069 case FCMP_UGT: return FCMP_OLE;
3070 case FCMP_ULT: return FCMP_OGE;
3071 case FCMP_UGE: return FCMP_OLT;
3072 case FCMP_ULE: return FCMP_OGT;
3073 case FCMP_ORD: return FCMP_UNO;
3074 case FCMP_UNO: return FCMP_ORD;
3075 case FCMP_TRUE: return FCMP_FALSE;
3076 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003077 }
3078}
3079
Reid Spencer266e42b2006-12-23 06:05:41 +00003080ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3081 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003082 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003083 case ICMP_EQ: case ICMP_NE:
3084 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3085 return pred;
3086 case ICMP_UGT: return ICMP_SGT;
3087 case ICMP_ULT: return ICMP_SLT;
3088 case ICMP_UGE: return ICMP_SGE;
3089 case ICMP_ULE: return ICMP_SLE;
3090 }
3091}
3092
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003093ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3094 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003095 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003096 case ICMP_EQ: case ICMP_NE:
3097 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3098 return pred;
3099 case ICMP_SGT: return ICMP_UGT;
3100 case ICMP_SLT: return ICMP_ULT;
3101 case ICMP_SGE: return ICMP_UGE;
3102 case ICMP_SLE: return ICMP_ULE;
3103 }
3104}
3105
Reid Spencer0286bc12007-02-28 22:00:54 +00003106/// Initialize a set of values that all satisfy the condition with C.
3107///
3108ConstantRange
3109ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3110 APInt Lower(C);
3111 APInt Upper(C);
3112 uint32_t BitWidth = C.getBitWidth();
3113 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003114 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00003115 case ICmpInst::ICMP_EQ: Upper++; break;
3116 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003117 case ICmpInst::ICMP_ULT:
3118 Lower = APInt::getMinValue(BitWidth);
3119 // Check for an empty-set condition.
3120 if (Lower == Upper)
3121 return ConstantRange(BitWidth, /*isFullSet=*/false);
3122 break;
3123 case ICmpInst::ICMP_SLT:
3124 Lower = APInt::getSignedMinValue(BitWidth);
3125 // Check for an empty-set condition.
3126 if (Lower == Upper)
3127 return ConstantRange(BitWidth, /*isFullSet=*/false);
3128 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003129 case ICmpInst::ICMP_UGT:
3130 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003131 // Check for an empty-set condition.
3132 if (Lower == Upper)
3133 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003134 break;
3135 case ICmpInst::ICMP_SGT:
3136 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003137 // Check for an empty-set condition.
3138 if (Lower == Upper)
3139 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003140 break;
3141 case ICmpInst::ICMP_ULE:
3142 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003143 // Check for a full-set condition.
3144 if (Lower == Upper)
3145 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003146 break;
3147 case ICmpInst::ICMP_SLE:
3148 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003149 // Check for a full-set condition.
3150 if (Lower == Upper)
3151 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003152 break;
3153 case ICmpInst::ICMP_UGE:
3154 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003155 // Check for a full-set condition.
3156 if (Lower == Upper)
3157 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003158 break;
3159 case ICmpInst::ICMP_SGE:
3160 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003161 // Check for a full-set condition.
3162 if (Lower == Upper)
3163 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003164 break;
3165 }
3166 return ConstantRange(Lower, Upper);
3167}
3168
Dan Gohman4e724382008-05-31 02:47:54 +00003169CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003170 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003171 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003172 case ICMP_EQ: case ICMP_NE:
3173 return pred;
3174 case ICMP_SGT: return ICMP_SLT;
3175 case ICMP_SLT: return ICMP_SGT;
3176 case ICMP_SGE: return ICMP_SLE;
3177 case ICMP_SLE: return ICMP_SGE;
3178 case ICMP_UGT: return ICMP_ULT;
3179 case ICMP_ULT: return ICMP_UGT;
3180 case ICMP_UGE: return ICMP_ULE;
3181 case ICMP_ULE: return ICMP_UGE;
3182
Reid Spencerd9436b62006-11-20 01:22:35 +00003183 case FCMP_FALSE: case FCMP_TRUE:
3184 case FCMP_OEQ: case FCMP_ONE:
3185 case FCMP_UEQ: case FCMP_UNE:
3186 case FCMP_ORD: case FCMP_UNO:
3187 return pred;
3188 case FCMP_OGT: return FCMP_OLT;
3189 case FCMP_OLT: return FCMP_OGT;
3190 case FCMP_OGE: return FCMP_OLE;
3191 case FCMP_OLE: return FCMP_OGE;
3192 case FCMP_UGT: return FCMP_ULT;
3193 case FCMP_ULT: return FCMP_UGT;
3194 case FCMP_UGE: return FCMP_ULE;
3195 case FCMP_ULE: return FCMP_UGE;
3196 }
3197}
3198
Reid Spencer266e42b2006-12-23 06:05:41 +00003199bool CmpInst::isUnsigned(unsigned short predicate) {
3200 switch (predicate) {
3201 default: return false;
3202 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3203 case ICmpInst::ICMP_UGE: return true;
3204 }
3205}
3206
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003207bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003208 switch (predicate) {
3209 default: return false;
3210 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3211 case ICmpInst::ICMP_SGE: return true;
3212 }
3213}
3214
3215bool CmpInst::isOrdered(unsigned short predicate) {
3216 switch (predicate) {
3217 default: return false;
3218 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3219 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3220 case FCmpInst::FCMP_ORD: return true;
3221 }
3222}
3223
3224bool CmpInst::isUnordered(unsigned short predicate) {
3225 switch (predicate) {
3226 default: return false;
3227 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3228 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3229 case FCmpInst::FCMP_UNO: return true;
3230 }
3231}
3232
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003233bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3234 switch(predicate) {
3235 default: return false;
3236 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3237 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3238 }
3239}
3240
3241bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3242 switch(predicate) {
3243 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3244 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3245 default: return false;
3246 }
3247}
3248
3249
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003250//===----------------------------------------------------------------------===//
3251// SwitchInst Implementation
3252//===----------------------------------------------------------------------===//
3253
Chris Lattnerbaf00152010-11-17 05:41:46 +00003254void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3255 assert(Value && Default && NumReserved);
3256 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003257 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003258 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003259
Gabor Greif2d3024d2008-05-26 21:33:52 +00003260 OperandList[0] = Value;
3261 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003262}
3263
Chris Lattner2195fc42007-02-24 00:55:48 +00003264/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3265/// switch on and a default destination. The number of additional cases can
3266/// be specified here to make memory allocation more efficient. This
3267/// constructor can also autoinsert before another instruction.
3268SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3269 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003270 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3271 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003272 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003273}
3274
3275/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3276/// switch on and a default destination. The number of additional cases can
3277/// be specified here to make memory allocation more efficient. This
3278/// constructor also autoinserts at the end of the specified BasicBlock.
3279SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3280 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003281 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3282 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003283 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003284}
3285
Misha Brukmanb1c93172005-04-21 23:48:37 +00003286SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003287 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3288 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3289 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003290 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003291 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003292 OL[i] = InOL[i];
3293 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003294 }
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003295 TheSubsets = SI.TheSubsets;
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003296 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003297}
3298
Gordon Henriksen14a55692007-12-10 02:14:30 +00003299SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003300 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003301}
3302
3303
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003304/// addCase - Add an entry to the switch instruction...
3305///
Chris Lattner47ac1872005-02-24 05:32:09 +00003306void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003307 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00003308
3309 // FIXME: Currently we work with ConstantInt based cases.
3310 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003311 Mapping.add(IntItem::fromConstantInt(OnVal));
3312 IntegersSubset CaseRanges = Mapping.getCase();
3313 addCase(CaseRanges, Dest);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00003314}
3315
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003316void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003317 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003318 unsigned OpNo = NumOperands;
3319 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003320 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003321 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003322 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003323 NumOperands = OpNo+2;
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003324
3325 SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3326
3327 CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3328 Case.updateCaseValueOperand(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003329 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003330}
3331
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003332/// removeCase - This method removes the specified case and its successor
3333/// from the switch instruction.
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003334void SwitchInst::removeCase(CaseIt& i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003335 unsigned idx = i.getCaseIndex();
3336
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003337 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003338
3339 unsigned NumOps = getNumOperands();
3340 Use *OL = OperandList;
3341
Jay Foad14277722011-02-01 09:22:34 +00003342 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003343 if (2 + (idx + 1) * 2 != NumOps) {
3344 OL[2 + idx * 2] = OL[NumOps - 2];
3345 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003346 }
3347
3348 // Nuke the last value.
3349 OL[NumOps-2].set(0);
3350 OL[NumOps-2+1].set(0);
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003351
3352 // Do the same with TheCases collection:
3353 if (i.SubsetIt != --TheSubsets.end()) {
3354 *i.SubsetIt = TheSubsets.back();
3355 TheSubsets.pop_back();
3356 } else {
3357 TheSubsets.pop_back();
3358 i.SubsetIt = TheSubsets.end();
3359 }
3360
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003361 NumOperands = NumOps-2;
3362}
3363
Jay Foade98f29d2011-04-01 08:00:58 +00003364/// growOperands - grow operands - This grows the operand list in response
3365/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003366///
Jay Foade98f29d2011-04-01 08:00:58 +00003367void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003368 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003369 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003370
3371 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003372 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003373 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003374 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003375 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003376 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003377 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003378 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003379}
3380
3381
3382BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3383 return getSuccessor(idx);
3384}
3385unsigned SwitchInst::getNumSuccessorsV() const {
3386 return getNumSuccessors();
3387}
3388void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3389 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003390}
Chris Lattnerf22be932004-10-15 23:52:53 +00003391
Chris Lattner3ed871f2009-10-27 19:13:16 +00003392//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003393// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003394//===----------------------------------------------------------------------===//
3395
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003396void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003397 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003398 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003399 ReservedSpace = 1+NumDests;
3400 NumOperands = 1;
3401 OperandList = allocHungoffUses(ReservedSpace);
3402
3403 OperandList[0] = Address;
3404}
3405
3406
Jay Foade98f29d2011-04-01 08:00:58 +00003407/// growOperands - grow operands - This grows the operand list in response
3408/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003409///
Jay Foade98f29d2011-04-01 08:00:58 +00003410void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003411 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003412 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003413
3414 ReservedSpace = NumOps;
3415 Use *NewOps = allocHungoffUses(NumOps);
3416 Use *OldOps = OperandList;
3417 for (unsigned i = 0; i != e; ++i)
3418 NewOps[i] = OldOps[i];
3419 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003420 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003421}
3422
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003423IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3424 Instruction *InsertBefore)
3425: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003426 0, 0, InsertBefore) {
3427 init(Address, NumCases);
3428}
3429
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003430IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3431 BasicBlock *InsertAtEnd)
3432: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003433 0, 0, InsertAtEnd) {
3434 init(Address, NumCases);
3435}
3436
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003437IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3438 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003439 allocHungoffUses(IBI.getNumOperands()),
3440 IBI.getNumOperands()) {
3441 Use *OL = OperandList, *InOL = IBI.OperandList;
3442 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3443 OL[i] = InOL[i];
3444 SubclassOptionalData = IBI.SubclassOptionalData;
3445}
3446
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003447IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003448 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003449}
3450
3451/// addDestination - Add a destination.
3452///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003453void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003454 unsigned OpNo = NumOperands;
3455 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003456 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003457 // Initialize some new operands.
3458 assert(OpNo < ReservedSpace && "Growing didn't work!");
3459 NumOperands = OpNo+1;
3460 OperandList[OpNo] = DestBB;
3461}
3462
3463/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003464/// indirectbr instruction.
3465void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003466 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3467
3468 unsigned NumOps = getNumOperands();
3469 Use *OL = OperandList;
3470
3471 // Replace this value with the last one.
3472 OL[idx+1] = OL[NumOps-1];
3473
3474 // Nuke the last value.
3475 OL[NumOps-1].set(0);
3476 NumOperands = NumOps-1;
3477}
3478
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003479BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003480 return getSuccessor(idx);
3481}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003482unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003483 return getNumSuccessors();
3484}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003485void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003486 setSuccessor(idx, B);
3487}
3488
3489//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003490// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003491//===----------------------------------------------------------------------===//
3492
Chris Lattnerf22be932004-10-15 23:52:53 +00003493// Define these methods here so vtables don't get emitted into every translation
3494// unit that uses these classes.
3495
Devang Patel11cf3f42009-10-27 22:16:29 +00003496GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3497 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003498}
3499
Devang Patel11cf3f42009-10-27 22:16:29 +00003500BinaryOperator *BinaryOperator::clone_impl() const {
3501 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003502}
3503
Devang Patel11cf3f42009-10-27 22:16:29 +00003504FCmpInst* FCmpInst::clone_impl() const {
3505 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003506}
3507
Devang Patel11cf3f42009-10-27 22:16:29 +00003508ICmpInst* ICmpInst::clone_impl() const {
3509 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003510}
3511
Devang Patel11cf3f42009-10-27 22:16:29 +00003512ExtractValueInst *ExtractValueInst::clone_impl() const {
3513 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003514}
3515
Devang Patel11cf3f42009-10-27 22:16:29 +00003516InsertValueInst *InsertValueInst::clone_impl() const {
3517 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003518}
3519
Devang Patel11cf3f42009-10-27 22:16:29 +00003520AllocaInst *AllocaInst::clone_impl() const {
3521 return new AllocaInst(getAllocatedType(),
3522 (Value*)getOperand(0),
3523 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003524}
3525
Devang Patel11cf3f42009-10-27 22:16:29 +00003526LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003527 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3528 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003529}
3530
Devang Patel11cf3f42009-10-27 22:16:29 +00003531StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003532 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003533 getAlignment(), getOrdering(), getSynchScope());
3534
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003535}
3536
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003537AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3538 AtomicCmpXchgInst *Result =
3539 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3540 getOrdering(), getSynchScope());
3541 Result->setVolatile(isVolatile());
3542 return Result;
3543}
3544
3545AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3546 AtomicRMWInst *Result =
3547 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3548 getOrdering(), getSynchScope());
3549 Result->setVolatile(isVolatile());
3550 return Result;
3551}
3552
Eli Friedmanfee02c62011-07-25 23:16:38 +00003553FenceInst *FenceInst::clone_impl() const {
3554 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3555}
3556
Devang Patel11cf3f42009-10-27 22:16:29 +00003557TruncInst *TruncInst::clone_impl() const {
3558 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003559}
3560
Devang Patel11cf3f42009-10-27 22:16:29 +00003561ZExtInst *ZExtInst::clone_impl() const {
3562 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003563}
3564
Devang Patel11cf3f42009-10-27 22:16:29 +00003565SExtInst *SExtInst::clone_impl() const {
3566 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003567}
3568
Devang Patel11cf3f42009-10-27 22:16:29 +00003569FPTruncInst *FPTruncInst::clone_impl() const {
3570 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003571}
3572
Devang Patel11cf3f42009-10-27 22:16:29 +00003573FPExtInst *FPExtInst::clone_impl() const {
3574 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003575}
3576
Devang Patel11cf3f42009-10-27 22:16:29 +00003577UIToFPInst *UIToFPInst::clone_impl() const {
3578 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003579}
3580
Devang Patel11cf3f42009-10-27 22:16:29 +00003581SIToFPInst *SIToFPInst::clone_impl() const {
3582 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003583}
3584
Devang Patel11cf3f42009-10-27 22:16:29 +00003585FPToUIInst *FPToUIInst::clone_impl() const {
3586 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003587}
3588
Devang Patel11cf3f42009-10-27 22:16:29 +00003589FPToSIInst *FPToSIInst::clone_impl() const {
3590 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003591}
3592
Devang Patel11cf3f42009-10-27 22:16:29 +00003593PtrToIntInst *PtrToIntInst::clone_impl() const {
3594 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003595}
3596
Devang Patel11cf3f42009-10-27 22:16:29 +00003597IntToPtrInst *IntToPtrInst::clone_impl() const {
3598 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003599}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003600
Devang Patel11cf3f42009-10-27 22:16:29 +00003601BitCastInst *BitCastInst::clone_impl() const {
3602 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003603}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003604
Devang Patel11cf3f42009-10-27 22:16:29 +00003605CallInst *CallInst::clone_impl() const {
3606 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003607}
3608
Devang Patel11cf3f42009-10-27 22:16:29 +00003609SelectInst *SelectInst::clone_impl() const {
3610 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003611}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003612
Devang Patel11cf3f42009-10-27 22:16:29 +00003613VAArgInst *VAArgInst::clone_impl() const {
3614 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003615}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003616
Devang Patel11cf3f42009-10-27 22:16:29 +00003617ExtractElementInst *ExtractElementInst::clone_impl() const {
3618 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003619}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003620
Devang Patel11cf3f42009-10-27 22:16:29 +00003621InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003622 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003623}
3624
Devang Patel11cf3f42009-10-27 22:16:29 +00003625ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003626 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003627}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003628
Devang Patel11cf3f42009-10-27 22:16:29 +00003629PHINode *PHINode::clone_impl() const {
3630 return new PHINode(*this);
3631}
3632
Bill Wendlingfae14752011-08-12 20:24:12 +00003633LandingPadInst *LandingPadInst::clone_impl() const {
3634 return new LandingPadInst(*this);
3635}
3636
Devang Patel11cf3f42009-10-27 22:16:29 +00003637ReturnInst *ReturnInst::clone_impl() const {
3638 return new(getNumOperands()) ReturnInst(*this);
3639}
3640
3641BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003642 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003643}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003644
Devang Patel11cf3f42009-10-27 22:16:29 +00003645SwitchInst *SwitchInst::clone_impl() const {
3646 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003647}
3648
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003649IndirectBrInst *IndirectBrInst::clone_impl() const {
3650 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003651}
3652
3653
Devang Patel11cf3f42009-10-27 22:16:29 +00003654InvokeInst *InvokeInst::clone_impl() const {
3655 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003656}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003657
Bill Wendlingf891bf82011-07-31 06:30:59 +00003658ResumeInst *ResumeInst::clone_impl() const {
3659 return new(1) ResumeInst(*this);
3660}
3661
Devang Patel11cf3f42009-10-27 22:16:29 +00003662UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003663 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003664 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003665}