blob: ded95349d4132be129cc34d822252719cf13b59b [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Function.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"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000022#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000023#include "llvm/Support/ConstantRange.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/ErrorHandling.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) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000334 AttributeSet PAL = getAttributes();
Bill Wendling722b26c2012-10-14 07:35:59 +0000335 PAL = PAL.addAttr(getContext(), 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) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000340 AttributeSet PAL = getAttributes();
Bill Wendling85a64c22012-10-14 06:39:53 +0000341 PAL = PAL.removeAttr(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000342 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000343}
344
Bill Wendlingff758fb2012-10-09 21:49:51 +0000345bool CallInst::hasFnAttr(Attributes::AttrVal A) const {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000346 if (AttributeList.getParamAttributes(AttributeSet::FunctionIndex)
Bill Wendlingfbd38fe2012-10-15 07:29:08 +0000347 .hasAttribute(A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000348 return true;
349 if (const Function *F = getCalledFunction())
Bill Wendlinge94d8432012-12-07 23:16:57 +0000350 return F->getParamAttributes(AttributeSet::FunctionIndex).hasAttribute(A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000351 return false;
352}
353
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000354bool CallInst::paramHasAttr(unsigned i, Attributes::AttrVal A) const {
355 if (AttributeList.getParamAttributes(i).hasAttribute(A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000356 return true;
357 if (const Function *F = getCalledFunction())
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000358 return F->getParamAttributes(i).hasAttribute(A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000359 return false;
360}
361
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000362/// IsConstantOne - Return true only if val is constant int 1
363static bool IsConstantOne(Value *val) {
364 assert(val && "IsConstantOne does not work with NULL val");
365 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
366}
367
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000368static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000369 BasicBlock *InsertAtEnd, Type *IntPtrTy,
370 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000371 Value *ArraySize, Function *MallocF,
372 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000373 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000374 "createMalloc needs either InsertBefore or InsertAtEnd");
375
376 // malloc(type) becomes:
377 // bitcast (i8* malloc(typeSize)) to type*
378 // malloc(type, arraySize) becomes:
379 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000380 if (!ArraySize)
381 ArraySize = ConstantInt::get(IntPtrTy, 1);
382 else if (ArraySize->getType() != IntPtrTy) {
383 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000384 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
385 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000386 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000387 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
388 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000389 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000390
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000391 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000392 if (IsConstantOne(AllocSize)) {
393 AllocSize = ArraySize; // Operand * 1 = Operand
394 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
395 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
396 false /*ZExt*/);
397 // Malloc arg is constant product of type size and array size
398 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
399 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000400 // Multiply type size by the array size...
401 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000402 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
403 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000404 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000405 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
406 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000407 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000408 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000409
Victor Hernandez788eaab2009-09-18 19:20:02 +0000410 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000411 // Create the call to Malloc.
412 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
413 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000414 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000415 Value *MallocFunc = MallocF;
416 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000417 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000418 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Chris Lattner229907c2011-07-18 04:54:35 +0000419 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000420 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000421 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000422 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000423 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000424 Result = MCall;
425 if (Result->getType() != AllocPtrType)
426 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000427 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000428 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000429 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000430 Result = MCall;
431 if (Result->getType() != AllocPtrType) {
432 InsertAtEnd->getInstList().push_back(MCall);
433 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000434 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000435 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000436 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000437 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000438 if (Function *F = dyn_cast<Function>(MallocFunc)) {
439 MCall->setCallingConv(F->getCallingConv());
440 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
441 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000442 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000443
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000444 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445}
446
447/// CreateMalloc - Generate the IR for a call to malloc:
448/// 1. Compute the malloc call's argument as the specified type's size,
449/// possibly multiplied by the array size if the array size is not
450/// constant 1.
451/// 2. Call malloc with that argument.
452/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000453Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000454 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000455 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000456 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000457 const Twine &Name) {
458 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000459 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000460}
461
462/// CreateMalloc - Generate the IR for a call to malloc:
463/// 1. Compute the malloc call's argument as the specified type's size,
464/// possibly multiplied by the array size if the array size is not
465/// constant 1.
466/// 2. Call malloc with that argument.
467/// 3. Bitcast the result of the malloc call to the specified type.
468/// Note: This function does not add the bitcast to the basic block, that is the
469/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000470Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000471 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000472 Value *AllocSize, Value *ArraySize,
473 Function *MallocF, const Twine &Name) {
474 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000475 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000476}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000477
Victor Hernandeze2971492009-10-24 04:23:03 +0000478static Instruction* createFree(Value* Source, Instruction *InsertBefore,
479 BasicBlock *InsertAtEnd) {
480 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
481 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000482 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000483 "Can not free something of nonpointer type!");
484
485 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
486 Module* M = BB->getParent()->getParent();
487
Chris Lattner229907c2011-07-18 04:54:35 +0000488 Type *VoidTy = Type::getVoidTy(M->getContext());
489 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000490 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000491 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000492 CallInst* Result = NULL;
493 Value *PtrCast = Source;
494 if (InsertBefore) {
495 if (Source->getType() != IntPtrTy)
496 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
497 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
498 } else {
499 if (Source->getType() != IntPtrTy)
500 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
501 Result = CallInst::Create(FreeFunc, PtrCast, "");
502 }
503 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000504 if (Function *F = dyn_cast<Function>(FreeFunc))
505 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000506
507 return Result;
508}
509
510/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000511Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
512 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000513}
514
515/// CreateFree - Generate the IR for a call to the builtin free function.
516/// Note: This function does not add the call to the basic block, that is the
517/// responsibility of the caller.
518Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
519 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
520 assert(FreeCall && "CreateFree did not create a CallInst");
521 return FreeCall;
522}
523
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000524//===----------------------------------------------------------------------===//
525// InvokeInst Implementation
526//===----------------------------------------------------------------------===//
527
528void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000529 ArrayRef<Value *> Args, const Twine &NameStr) {
530 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000531 Op<-3>() = Fn;
532 Op<-2>() = IfNormal;
533 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000534
535#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000536 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000537 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000538
Jay Foad5bd375a2011-07-15 08:37:34 +0000539 assert(((Args.size() == FTy->getNumParams()) ||
540 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000541 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000542
Jay Foad5bd375a2011-07-15 08:37:34 +0000543 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000544 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000545 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000546 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000547#endif
548
549 std::copy(Args.begin(), Args.end(), op_begin());
550 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000551}
552
Misha Brukmanb1c93172005-04-21 23:48:37 +0000553InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000554 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000555 OperandTraits<InvokeInst>::op_end(this)
556 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000557 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000558 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000559 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000560 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000561 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000562}
563
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000564BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
565 return getSuccessor(idx);
566}
567unsigned InvokeInst::getNumSuccessorsV() const {
568 return getNumSuccessors();
569}
570void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
571 return setSuccessor(idx, B);
572}
573
Bill Wendlingff758fb2012-10-09 21:49:51 +0000574bool InvokeInst::hasFnAttr(Attributes::AttrVal A) const {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000575 if (AttributeList.getParamAttributes(AttributeSet::FunctionIndex).
Bill Wendlingfbd38fe2012-10-15 07:29:08 +0000576 hasAttribute(A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000577 return true;
578 if (const Function *F = getCalledFunction())
Bill Wendlinge94d8432012-12-07 23:16:57 +0000579 return F->getParamAttributes(AttributeSet::FunctionIndex).hasAttribute(A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000580 return false;
581}
582
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000583bool InvokeInst::paramHasAttr(unsigned i, Attributes::AttrVal A) const {
584 if (AttributeList.getParamAttributes(i).hasAttribute(A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000585 return true;
586 if (const Function *F = getCalledFunction())
Bill Wendling8ccd6ca2012-10-09 21:38:14 +0000587 return F->getParamAttributes(i).hasAttribute(A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000588 return false;
589}
590
Devang Patel4c758ea2008-09-25 21:00:45 +0000591void InvokeInst::addAttribute(unsigned i, Attributes attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000592 AttributeSet PAL = getAttributes();
Bill Wendling722b26c2012-10-14 07:35:59 +0000593 PAL = PAL.addAttr(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000594 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000595}
596
Devang Patel4c758ea2008-09-25 21:00:45 +0000597void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000598 AttributeSet PAL = getAttributes();
Bill Wendling85a64c22012-10-14 06:39:53 +0000599 PAL = PAL.removeAttr(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000600 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000601}
602
Bill Wendlingfae14752011-08-12 20:24:12 +0000603LandingPadInst *InvokeInst::getLandingPadInst() const {
604 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
605}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000606
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607//===----------------------------------------------------------------------===//
608// ReturnInst Implementation
609//===----------------------------------------------------------------------===//
610
Chris Lattner2195fc42007-02-24 00:55:48 +0000611ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000612 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000613 OperandTraits<ReturnInst>::op_end(this) -
614 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000615 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000616 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000617 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000618 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000619}
620
Owen Anderson55f1c092009-08-13 21:58:54 +0000621ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
622 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000623 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
624 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000625 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000626 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000627}
Owen Anderson55f1c092009-08-13 21:58:54 +0000628ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
629 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000630 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
631 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000632 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000633 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000634}
Owen Anderson55f1c092009-08-13 21:58:54 +0000635ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
636 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000637 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000638}
639
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000640unsigned ReturnInst::getNumSuccessorsV() const {
641 return getNumSuccessors();
642}
643
Devang Patelae682fb2008-02-26 17:56:20 +0000644/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
645/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000646void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000647 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000648}
649
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000650BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000651 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000652}
653
Devang Patel59643e52008-02-23 00:35:18 +0000654ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000655}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000657//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000658// ResumeInst Implementation
659//===----------------------------------------------------------------------===//
660
661ResumeInst::ResumeInst(const ResumeInst &RI)
662 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
663 OperandTraits<ResumeInst>::op_begin(this), 1) {
664 Op<0>() = RI.Op<0>();
665}
666
667ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
668 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
669 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
670 Op<0>() = Exn;
671}
672
673ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
674 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
675 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
676 Op<0>() = Exn;
677}
678
679unsigned ResumeInst::getNumSuccessorsV() const {
680 return getNumSuccessors();
681}
682
683void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
684 llvm_unreachable("ResumeInst has no successors!");
685}
686
687BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
688 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000689}
690
691//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000692// UnreachableInst Implementation
693//===----------------------------------------------------------------------===//
694
Owen Anderson55f1c092009-08-13 21:58:54 +0000695UnreachableInst::UnreachableInst(LLVMContext &Context,
696 Instruction *InsertBefore)
697 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
698 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000699}
Owen Anderson55f1c092009-08-13 21:58:54 +0000700UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
701 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
702 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000703}
704
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705unsigned UnreachableInst::getNumSuccessorsV() const {
706 return getNumSuccessors();
707}
708
709void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000710 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711}
712
713BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000714 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000715}
716
717//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718// BranchInst Implementation
719//===----------------------------------------------------------------------===//
720
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721void BranchInst::AssertOK() {
722 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000723 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Chris Lattner2195fc42007-02-24 00:55:48 +0000727BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000728 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000729 OperandTraits<BranchInst>::op_end(this) - 1,
730 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000731 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000732 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000733}
734BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
735 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000736 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000737 OperandTraits<BranchInst>::op_end(this) - 3,
738 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000739 Op<-1>() = IfTrue;
740 Op<-2>() = IfFalse;
741 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000742#ifndef NDEBUG
743 AssertOK();
744#endif
745}
746
747BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000748 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000749 OperandTraits<BranchInst>::op_end(this) - 1,
750 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000751 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000752 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000753}
754
755BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
756 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000757 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000758 OperandTraits<BranchInst>::op_end(this) - 3,
759 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000760 Op<-1>() = IfTrue;
761 Op<-2>() = IfFalse;
762 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000763#ifndef NDEBUG
764 AssertOK();
765#endif
766}
767
768
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000769BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000770 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000771 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
772 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000773 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774 if (BI.getNumOperands() != 1) {
775 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000776 Op<-3>() = BI.Op<-3>();
777 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000778 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000779 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780}
781
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000782void BranchInst::swapSuccessors() {
783 assert(isConditional() &&
784 "Cannot swap successors of an unconditional branch");
785 Op<-1>().swap(Op<-2>());
786
787 // Update profile metadata if present and it matches our structural
788 // expectations.
789 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
790 if (!ProfileData || ProfileData->getNumOperands() != 3)
791 return;
792
793 // The first operand is the name. Fetch them backwards and build a new one.
794 Value *Ops[] = {
795 ProfileData->getOperand(0),
796 ProfileData->getOperand(2),
797 ProfileData->getOperand(1)
798 };
799 setMetadata(LLVMContext::MD_prof,
800 MDNode::get(ProfileData->getContext(), Ops));
801}
802
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000803BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
804 return getSuccessor(idx);
805}
806unsigned BranchInst::getNumSuccessorsV() const {
807 return getNumSuccessors();
808}
809void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
810 setSuccessor(idx, B);
811}
812
813
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000814//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000815// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000816//===----------------------------------------------------------------------===//
817
Owen Andersonb6b25302009-07-14 23:09:55 +0000818static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000819 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000820 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000821 else {
822 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000823 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000824 assert(Amt->getType()->isIntegerTy() &&
825 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000826 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000827 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828}
829
Chris Lattner229907c2011-07-18 04:54:35 +0000830AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000831 const Twine &Name, Instruction *InsertBefore)
832 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
833 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
834 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000835 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000836 setName(Name);
837}
838
Chris Lattner229907c2011-07-18 04:54:35 +0000839AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000840 const Twine &Name, BasicBlock *InsertAtEnd)
841 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
842 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
843 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000844 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000845 setName(Name);
846}
847
Chris Lattner229907c2011-07-18 04:54:35 +0000848AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000849 Instruction *InsertBefore)
850 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
851 getAISize(Ty->getContext(), 0), InsertBefore) {
852 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000853 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000854 setName(Name);
855}
856
Chris Lattner229907c2011-07-18 04:54:35 +0000857AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000858 BasicBlock *InsertAtEnd)
859 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
860 getAISize(Ty->getContext(), 0), InsertAtEnd) {
861 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000862 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000863 setName(Name);
864}
865
Chris Lattner229907c2011-07-18 04:54:35 +0000866AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000867 const Twine &Name, Instruction *InsertBefore)
868 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000869 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000870 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000871 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000872 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000873}
874
Chris Lattner229907c2011-07-18 04:54:35 +0000875AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000876 const Twine &Name, BasicBlock *InsertAtEnd)
877 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000878 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000879 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000880 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000881 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000882}
883
Gordon Henriksen14a55692007-12-10 02:14:30 +0000884// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000885AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000886}
887
Victor Hernandez8acf2952009-10-23 21:09:37 +0000888void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000889 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000890 assert(Align <= MaximumAlignment &&
891 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000892 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000893 assert(getAlignment() == Align && "Alignment representation error!");
894}
895
Victor Hernandez8acf2952009-10-23 21:09:37 +0000896bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000897 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000898 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000899 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900}
901
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000902Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000903 return getType()->getElementType();
904}
905
Chris Lattner8b291e62008-11-26 02:54:17 +0000906/// isStaticAlloca - Return true if this alloca is in the entry block of the
907/// function and is a constant size. If so, the code generator will fold it
908/// into the prolog/epilog code, so it is basically free.
909bool AllocaInst::isStaticAlloca() const {
910 // Must be constant size.
911 if (!isa<ConstantInt>(getArraySize())) return false;
912
913 // Must be in the entry block.
914 const BasicBlock *Parent = getParent();
915 return Parent == &Parent->getParent()->front();
916}
917
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000918//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000919// LoadInst Implementation
920//===----------------------------------------------------------------------===//
921
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000923 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000924 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000925 assert(!(isAtomic() && getAlignment() == 0) &&
926 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000927}
928
Daniel Dunbar4975db62009-07-25 04:41:11 +0000929LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000930 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000931 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000932 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000933 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000934 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000935 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000936 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000937}
938
Daniel Dunbar4975db62009-07-25 04:41:11 +0000939LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000940 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000941 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000942 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000943 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000944 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000945 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000946 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000947}
948
Daniel Dunbar4975db62009-07-25 04:41:11 +0000949LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000950 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000951 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000952 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000953 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000954 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000955 setAtomic(NotAtomic);
956 AssertOK();
957 setName(Name);
958}
959
960LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
961 BasicBlock *InsertAE)
962 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
963 Load, Ptr, InsertAE) {
964 setVolatile(isVolatile);
965 setAlignment(0);
966 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +0000967 AssertOK();
968 setName(Name);
969}
970
Daniel Dunbar4975db62009-07-25 04:41:11 +0000971LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000972 unsigned Align, Instruction *InsertBef)
973 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
974 Load, Ptr, InsertBef) {
975 setVolatile(isVolatile);
976 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000977 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000978 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000979 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980}
981
Daniel Dunbar4975db62009-07-25 04:41:11 +0000982LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000983 unsigned Align, BasicBlock *InsertAE)
984 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
985 Load, Ptr, InsertAE) {
986 setVolatile(isVolatile);
987 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +0000988 setAtomic(NotAtomic);
Dan Gohman68659282007-07-18 20:51:11 +0000989 AssertOK();
990 setName(Name);
991}
992
Eli Friedman59b66882011-08-09 23:02:53 +0000993LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
994 unsigned Align, AtomicOrdering Order,
995 SynchronizationScope SynchScope,
996 Instruction *InsertBef)
997 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
998 Load, Ptr, InsertBef) {
999 setVolatile(isVolatile);
1000 setAlignment(Align);
1001 setAtomic(Order, SynchScope);
1002 AssertOK();
1003 setName(Name);
1004}
1005
1006LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1007 unsigned Align, AtomicOrdering Order,
1008 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001009 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001010 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001011 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001012 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001013 setAlignment(Align);
1014 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001015 AssertOK();
1016 setName(Name);
1017}
1018
Daniel Dunbar27096822009-08-11 18:11:15 +00001019LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1020 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1021 Load, Ptr, InsertBef) {
1022 setVolatile(false);
1023 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001024 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001025 AssertOK();
1026 if (Name && Name[0]) setName(Name);
1027}
1028
1029LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1030 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1031 Load, Ptr, InsertAE) {
1032 setVolatile(false);
1033 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001034 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001035 AssertOK();
1036 if (Name && Name[0]) setName(Name);
1037}
1038
1039LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1040 Instruction *InsertBef)
1041: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1042 Load, Ptr, InsertBef) {
1043 setVolatile(isVolatile);
1044 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001045 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001046 AssertOK();
1047 if (Name && Name[0]) setName(Name);
1048}
1049
1050LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1051 BasicBlock *InsertAE)
1052 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1053 Load, Ptr, InsertAE) {
1054 setVolatile(isVolatile);
1055 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001056 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001057 AssertOK();
1058 if (Name && Name[0]) setName(Name);
1059}
1060
Christopher Lamb84485702007-04-22 19:24:39 +00001061void LoadInst::setAlignment(unsigned Align) {
1062 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001063 assert(Align <= MaximumAlignment &&
1064 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001065 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001066 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001067 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001068}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001069
1070//===----------------------------------------------------------------------===//
1071// StoreInst Implementation
1072//===----------------------------------------------------------------------===//
1073
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001074void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001075 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001076 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001077 "Ptr must have pointer type!");
1078 assert(getOperand(0)->getType() ==
1079 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001080 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001081 assert(!(isAtomic() && getAlignment() == 0) &&
1082 "Alignment required for atomic load");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001083}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001084
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001085
1086StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001087 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001088 OperandTraits<StoreInst>::op_begin(this),
1089 OperandTraits<StoreInst>::operands(this),
1090 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001091 Op<0>() = val;
1092 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001093 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001094 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001095 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001096 AssertOK();
1097}
1098
1099StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001100 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001101 OperandTraits<StoreInst>::op_begin(this),
1102 OperandTraits<StoreInst>::operands(this),
1103 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001104 Op<0>() = val;
1105 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001106 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001107 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001108 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001109 AssertOK();
1110}
1111
Misha Brukmanb1c93172005-04-21 23:48:37 +00001112StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001113 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001114 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001115 OperandTraits<StoreInst>::op_begin(this),
1116 OperandTraits<StoreInst>::operands(this),
1117 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001118 Op<0>() = val;
1119 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001120 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001121 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001122 setAtomic(NotAtomic);
Christopher Lamb84485702007-04-22 19:24:39 +00001123 AssertOK();
1124}
1125
1126StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1127 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001128 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001129 OperandTraits<StoreInst>::op_begin(this),
1130 OperandTraits<StoreInst>::operands(this),
1131 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001132 Op<0>() = val;
1133 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001134 setVolatile(isVolatile);
1135 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001136 setAtomic(NotAtomic);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001137 AssertOK();
1138}
1139
Misha Brukmanb1c93172005-04-21 23:48:37 +00001140StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001141 unsigned Align, AtomicOrdering Order,
1142 SynchronizationScope SynchScope,
1143 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001144 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001145 OperandTraits<StoreInst>::op_begin(this),
1146 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001147 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001148 Op<0>() = val;
1149 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001150 setVolatile(isVolatile);
1151 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001152 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001153 AssertOK();
1154}
1155
1156StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001157 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001158 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001159 OperandTraits<StoreInst>::op_begin(this),
1160 OperandTraits<StoreInst>::operands(this),
1161 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001162 Op<0>() = val;
1163 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001164 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001165 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001166 setAtomic(NotAtomic);
1167 AssertOK();
1168}
1169
1170StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1171 unsigned Align, BasicBlock *InsertAtEnd)
1172 : Instruction(Type::getVoidTy(val->getContext()), Store,
1173 OperandTraits<StoreInst>::op_begin(this),
1174 OperandTraits<StoreInst>::operands(this),
1175 InsertAtEnd) {
1176 Op<0>() = val;
1177 Op<1>() = addr;
1178 setVolatile(isVolatile);
1179 setAlignment(Align);
1180 setAtomic(NotAtomic);
1181 AssertOK();
1182}
1183
1184StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1185 unsigned Align, AtomicOrdering Order,
1186 SynchronizationScope SynchScope,
1187 BasicBlock *InsertAtEnd)
1188 : Instruction(Type::getVoidTy(val->getContext()), Store,
1189 OperandTraits<StoreInst>::op_begin(this),
1190 OperandTraits<StoreInst>::operands(this),
1191 InsertAtEnd) {
1192 Op<0>() = val;
1193 Op<1>() = addr;
1194 setVolatile(isVolatile);
1195 setAlignment(Align);
1196 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001197 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001198}
1199
Christopher Lamb84485702007-04-22 19:24:39 +00001200void StoreInst::setAlignment(unsigned Align) {
1201 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001202 assert(Align <= MaximumAlignment &&
1203 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001204 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001205 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001206 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001207}
1208
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001209//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001210// AtomicCmpXchgInst Implementation
1211//===----------------------------------------------------------------------===//
1212
1213void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1214 AtomicOrdering Ordering,
1215 SynchronizationScope SynchScope) {
1216 Op<0>() = Ptr;
1217 Op<1>() = Cmp;
1218 Op<2>() = NewVal;
1219 setOrdering(Ordering);
1220 setSynchScope(SynchScope);
1221
1222 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1223 "All operands must be non-null!");
1224 assert(getOperand(0)->getType()->isPointerTy() &&
1225 "Ptr must have pointer type!");
1226 assert(getOperand(1)->getType() ==
1227 cast<PointerType>(getOperand(0)->getType())->getElementType()
1228 && "Ptr must be a pointer to Cmp type!");
1229 assert(getOperand(2)->getType() ==
1230 cast<PointerType>(getOperand(0)->getType())->getElementType()
1231 && "Ptr must be a pointer to NewVal type!");
1232 assert(Ordering != NotAtomic &&
1233 "AtomicCmpXchg instructions must be atomic!");
1234}
1235
1236AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1237 AtomicOrdering Ordering,
1238 SynchronizationScope SynchScope,
1239 Instruction *InsertBefore)
1240 : Instruction(Cmp->getType(), AtomicCmpXchg,
1241 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1242 OperandTraits<AtomicCmpXchgInst>::operands(this),
1243 InsertBefore) {
1244 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1245}
1246
1247AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1248 AtomicOrdering Ordering,
1249 SynchronizationScope SynchScope,
1250 BasicBlock *InsertAtEnd)
1251 : Instruction(Cmp->getType(), AtomicCmpXchg,
1252 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1253 OperandTraits<AtomicCmpXchgInst>::operands(this),
1254 InsertAtEnd) {
1255 Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1256}
1257
1258//===----------------------------------------------------------------------===//
1259// AtomicRMWInst Implementation
1260//===----------------------------------------------------------------------===//
1261
1262void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1263 AtomicOrdering Ordering,
1264 SynchronizationScope SynchScope) {
1265 Op<0>() = Ptr;
1266 Op<1>() = Val;
1267 setOperation(Operation);
1268 setOrdering(Ordering);
1269 setSynchScope(SynchScope);
1270
1271 assert(getOperand(0) && getOperand(1) &&
1272 "All operands must be non-null!");
1273 assert(getOperand(0)->getType()->isPointerTy() &&
1274 "Ptr must have pointer type!");
1275 assert(getOperand(1)->getType() ==
1276 cast<PointerType>(getOperand(0)->getType())->getElementType()
1277 && "Ptr must be a pointer to Val type!");
1278 assert(Ordering != NotAtomic &&
1279 "AtomicRMW instructions must be atomic!");
1280}
1281
1282AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1283 AtomicOrdering Ordering,
1284 SynchronizationScope SynchScope,
1285 Instruction *InsertBefore)
1286 : Instruction(Val->getType(), AtomicRMW,
1287 OperandTraits<AtomicRMWInst>::op_begin(this),
1288 OperandTraits<AtomicRMWInst>::operands(this),
1289 InsertBefore) {
1290 Init(Operation, Ptr, Val, Ordering, SynchScope);
1291}
1292
1293AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1294 AtomicOrdering Ordering,
1295 SynchronizationScope SynchScope,
1296 BasicBlock *InsertAtEnd)
1297 : Instruction(Val->getType(), AtomicRMW,
1298 OperandTraits<AtomicRMWInst>::op_begin(this),
1299 OperandTraits<AtomicRMWInst>::operands(this),
1300 InsertAtEnd) {
1301 Init(Operation, Ptr, Val, Ordering, SynchScope);
1302}
1303
1304//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001305// FenceInst Implementation
1306//===----------------------------------------------------------------------===//
1307
1308FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1309 SynchronizationScope SynchScope,
1310 Instruction *InsertBefore)
1311 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1312 setOrdering(Ordering);
1313 setSynchScope(SynchScope);
1314}
1315
1316FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1317 SynchronizationScope SynchScope,
1318 BasicBlock *InsertAtEnd)
1319 : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1320 setOrdering(Ordering);
1321 setSynchScope(SynchScope);
1322}
1323
1324//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001325// GetElementPtrInst Implementation
1326//===----------------------------------------------------------------------===//
1327
Jay Foadd1b78492011-07-25 09:48:08 +00001328void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001329 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001330 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1331 OperandList[0] = Ptr;
1332 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001333 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001334}
1335
Gabor Greiff6caff662008-05-10 08:32:32 +00001336GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001337 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001338 OperandTraits<GetElementPtrInst>::op_end(this)
1339 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001340 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001341 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001342 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001343}
1344
Chris Lattner6090a422009-03-09 04:46:40 +00001345/// getIndexedType - Returns the type of the element that would be accessed with
1346/// a gep instruction with the specified parameters.
1347///
1348/// The Idxs pointer should point to a continuous piece of memory containing the
1349/// indices, either as Value* or uint64_t.
1350///
1351/// A null type is returned if the indices are invalid for the specified
1352/// pointer type.
1353///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001354template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001355static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Duncan Sandse6beec62012-11-13 12:59:33 +00001356 PointerType *PTy = dyn_cast<PointerType>(Ptr->getScalarType());
Dan Gohman12fce772008-05-15 19:50:34 +00001357 if (!PTy) return 0; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001358 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001359
Chris Lattner6090a422009-03-09 04:46:40 +00001360 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001361 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001362 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001363
Chris Lattner6090a422009-03-09 04:46:40 +00001364 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001365 // it cannot be 'stepped over'.
1366 if (!Agg->isSized())
Chris Lattner6090a422009-03-09 04:46:40 +00001367 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001368
Dan Gohman1ecaf452008-05-31 00:58:22 +00001369 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001370 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001371 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001372 if (!CT || CT->isPointerTy()) return 0;
Jay Foadd1b78492011-07-25 09:48:08 +00001373 IndexTy Index = IdxList[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001374 if (!CT->indexValid(Index)) return 0;
1375 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001376 }
Jay Foadd1b78492011-07-25 09:48:08 +00001377 return CurIdx == IdxList.size() ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001378}
1379
Jay Foadd1b78492011-07-25 09:48:08 +00001380Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1381 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001382}
1383
Chris Lattner229907c2011-07-18 04:54:35 +00001384Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001385 ArrayRef<Constant *> IdxList) {
1386 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001387}
1388
Jay Foadd1b78492011-07-25 09:48:08 +00001389Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1390 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001391}
1392
Chris Lattner45f15572007-04-14 00:12:57 +00001393/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1394/// zeros. If so, the result pointer and the first operand have the same
1395/// value, just potentially different types.
1396bool GetElementPtrInst::hasAllZeroIndices() const {
1397 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1398 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1399 if (!CI->isZero()) return false;
1400 } else {
1401 return false;
1402 }
1403 }
1404 return true;
1405}
1406
Chris Lattner27058292007-04-27 20:35:56 +00001407/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1408/// constant integers. If so, the result pointer and the first operand have
1409/// a constant offset between them.
1410bool GetElementPtrInst::hasAllConstantIndices() const {
1411 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1412 if (!isa<ConstantInt>(getOperand(i)))
1413 return false;
1414 }
1415 return true;
1416}
1417
Dan Gohman1b849082009-09-07 23:54:19 +00001418void GetElementPtrInst::setIsInBounds(bool B) {
1419 cast<GEPOperator>(this)->setIsInBounds(B);
1420}
Chris Lattner45f15572007-04-14 00:12:57 +00001421
Nick Lewycky28a5f252009-09-27 21:33:04 +00001422bool GetElementPtrInst::isInBounds() const {
1423 return cast<GEPOperator>(this)->isInBounds();
1424}
1425
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001426//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001427// ExtractElementInst Implementation
1428//===----------------------------------------------------------------------===//
1429
1430ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001431 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001432 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001433 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001434 ExtractElement,
1435 OperandTraits<ExtractElementInst>::op_begin(this),
1436 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001437 assert(isValidOperands(Val, Index) &&
1438 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001439 Op<0>() = Val;
1440 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001441 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001442}
1443
1444ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001445 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001446 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001447 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001448 ExtractElement,
1449 OperandTraits<ExtractElementInst>::op_begin(this),
1450 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001451 assert(isValidOperands(Val, Index) &&
1452 "Invalid extractelement instruction operands!");
1453
Gabor Greif2d3024d2008-05-26 21:33:52 +00001454 Op<0>() = Val;
1455 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001456 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001457}
1458
Chris Lattner65511ff2006-10-05 06:24:58 +00001459
Chris Lattner54865b32006-04-08 04:05:48 +00001460bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001461 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001462 return false;
1463 return true;
1464}
1465
1466
Robert Bocchino23004482006-01-10 19:05:34 +00001467//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001468// InsertElementInst Implementation
1469//===----------------------------------------------------------------------===//
1470
Chris Lattner54865b32006-04-08 04:05:48 +00001471InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001472 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001473 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001474 : Instruction(Vec->getType(), InsertElement,
1475 OperandTraits<InsertElementInst>::op_begin(this),
1476 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001477 assert(isValidOperands(Vec, Elt, Index) &&
1478 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001479 Op<0>() = Vec;
1480 Op<1>() = Elt;
1481 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001482 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001483}
1484
Chris Lattner54865b32006-04-08 04:05:48 +00001485InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001486 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001487 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001488 : Instruction(Vec->getType(), InsertElement,
1489 OperandTraits<InsertElementInst>::op_begin(this),
1490 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001491 assert(isValidOperands(Vec, Elt, Index) &&
1492 "Invalid insertelement instruction operands!");
1493
Gabor Greif2d3024d2008-05-26 21:33:52 +00001494 Op<0>() = Vec;
1495 Op<1>() = Elt;
1496 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001497 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001498}
1499
Chris Lattner54865b32006-04-08 04:05:48 +00001500bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1501 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001502 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001503 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001504
Reid Spencerd84d35b2007-02-15 02:26:10 +00001505 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001506 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001507
Duncan Sands9dff9be2010-02-15 16:12:20 +00001508 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001509 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001510 return true;
1511}
1512
1513
Robert Bocchinoca27f032006-01-17 20:07:22 +00001514//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001515// ShuffleVectorInst Implementation
1516//===----------------------------------------------------------------------===//
1517
1518ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001519 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001520 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001521: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001522 cast<VectorType>(Mask->getType())->getNumElements()),
1523 ShuffleVector,
1524 OperandTraits<ShuffleVectorInst>::op_begin(this),
1525 OperandTraits<ShuffleVectorInst>::operands(this),
1526 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001527 assert(isValidOperands(V1, V2, Mask) &&
1528 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001529 Op<0>() = V1;
1530 Op<1>() = V2;
1531 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001532 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001533}
1534
1535ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001536 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001537 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001538: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1539 cast<VectorType>(Mask->getType())->getNumElements()),
1540 ShuffleVector,
1541 OperandTraits<ShuffleVectorInst>::op_begin(this),
1542 OperandTraits<ShuffleVectorInst>::operands(this),
1543 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001544 assert(isValidOperands(V1, V2, Mask) &&
1545 "Invalid shuffle vector instruction operands!");
1546
Gabor Greif2d3024d2008-05-26 21:33:52 +00001547 Op<0>() = V1;
1548 Op<1>() = V2;
1549 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001550 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001551}
1552
Mon P Wang25f01062008-11-10 04:46:22 +00001553bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001554 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001555 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001556 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001557 return false;
1558
Chris Lattner1dcb6542012-01-25 23:49:49 +00001559 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001560 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001561 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001562 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001563
1564 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001565 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1566 return true;
1567
Nate Begeman60a31c32010-08-13 00:16:46 +00001568 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001569 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Nate Begeman60a31c32010-08-13 00:16:46 +00001570 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001571 if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1572 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001573 return false;
1574 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1575 return false;
1576 }
1577 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001578 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001579 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001580
1581 if (const ConstantDataSequential *CDS =
1582 dyn_cast<ConstantDataSequential>(Mask)) {
1583 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1584 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1585 if (CDS->getElementAsInteger(i) >= V1Size*2)
1586 return false;
1587 return true;
1588 }
1589
1590 // The bitcode reader can create a place holder for a forward reference
1591 // used as the shuffle mask. When this occurs, the shuffle mask will
1592 // fall into this case and fail. To avoid this error, do this bit of
1593 // ugliness to allow such a mask pass.
1594 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1595 if (CE->getOpcode() == Instruction::UserOp1)
1596 return true;
1597
1598 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001599}
1600
Chris Lattnerf724e342008-03-02 05:28:33 +00001601/// getMaskValue - Return the index from the shuffle mask for the specified
1602/// output result. This is either -1 if the element is undef or a number less
1603/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001604int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1605 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1606 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001607 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001608 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001609 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001610 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001611 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001612}
1613
Chris Lattner1dcb6542012-01-25 23:49:49 +00001614/// getShuffleMask - Return the full mask for this instruction, where each
1615/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001616void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1617 SmallVectorImpl<int> &Result) {
1618 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001619
Chris Lattnercf129702012-01-26 02:51:13 +00001620 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001621 for (unsigned i = 0; i != NumElts; ++i)
1622 Result.push_back(CDS->getElementAsInteger(i));
1623 return;
1624 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001625 for (unsigned i = 0; i != NumElts; ++i) {
1626 Constant *C = Mask->getAggregateElement(i);
1627 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001628 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001629 }
1630}
1631
1632
Dan Gohman12fce772008-05-15 19:50:34 +00001633//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001634// InsertValueInst Class
1635//===----------------------------------------------------------------------===//
1636
Jay Foad57aa6362011-07-13 10:26:04 +00001637void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1638 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001639 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001640
1641 // There's no fundamental reason why we require at least one index
1642 // (other than weirdness with &*IdxBegin being invalid; see
1643 // getelementptr's init routine for example). But there's no
1644 // present need to support it.
1645 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1646
1647 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001648 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001649 Op<0>() = Agg;
1650 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001651
Jay Foad57aa6362011-07-13 10:26:04 +00001652 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001653 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001654}
1655
1656InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001657 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001658 OperandTraits<InsertValueInst>::op_begin(this), 2),
1659 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001660 Op<0>() = IVI.getOperand(0);
1661 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001662 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001663}
1664
1665//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001666// ExtractValueInst Class
1667//===----------------------------------------------------------------------===//
1668
Jay Foad57aa6362011-07-13 10:26:04 +00001669void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001670 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001671
Jay Foad57aa6362011-07-13 10:26:04 +00001672 // There's no fundamental reason why we require at least one index.
1673 // But there's no present need to support it.
1674 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001675
Jay Foad57aa6362011-07-13 10:26:04 +00001676 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001677 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001678}
1679
1680ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001681 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001682 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001683 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001684}
1685
Dan Gohman12fce772008-05-15 19:50:34 +00001686// getIndexedType - Returns the type of the element that would be extracted
1687// with an extractvalue instruction with the specified parameters.
1688//
1689// A null type is returned if the indices are invalid for the specified
1690// pointer type.
1691//
Chris Lattner229907c2011-07-18 04:54:35 +00001692Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001693 ArrayRef<unsigned> Idxs) {
1694 for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001695 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001696 // We can't use CompositeType::indexValid(Index) here.
1697 // indexValid() always returns true for arrays because getelementptr allows
1698 // out-of-bounds indices. Since we don't allow those for extractvalue and
1699 // insertvalue we need to check array indexing manually.
1700 // Since the only other types we can index into are struct types it's just
1701 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001702 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001703 if (Index >= AT->getNumElements())
1704 return 0;
Chris Lattner229907c2011-07-18 04:54:35 +00001705 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001706 if (Index >= ST->getNumElements())
1707 return 0;
1708 } else {
1709 // Not a valid type to index into.
1710 return 0;
1711 }
1712
1713 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001714 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001715 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001716}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001717
1718//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001719// BinaryOperator Class
1720//===----------------------------------------------------------------------===//
1721
Chris Lattner2195fc42007-02-24 00:55:48 +00001722BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001723 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001724 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001725 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001726 OperandTraits<BinaryOperator>::op_begin(this),
1727 OperandTraits<BinaryOperator>::operands(this),
1728 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001729 Op<0>() = S1;
1730 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001731 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001732 setName(Name);
1733}
1734
1735BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001736 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001737 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001738 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001739 OperandTraits<BinaryOperator>::op_begin(this),
1740 OperandTraits<BinaryOperator>::operands(this),
1741 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001742 Op<0>() = S1;
1743 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001744 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001745 setName(Name);
1746}
1747
1748
1749void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001750 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001751 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001752 assert(LHS->getType() == RHS->getType() &&
1753 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001754#ifndef NDEBUG
1755 switch (iType) {
1756 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001757 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001758 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001759 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001760 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001761 "Tried to create an integer operation on a non-integer type!");
1762 break;
1763 case FAdd: case FSub:
1764 case FMul:
1765 assert(getType() == LHS->getType() &&
1766 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001767 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001768 "Tried to create a floating-point operation on a "
1769 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001770 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001771 case UDiv:
1772 case SDiv:
1773 assert(getType() == LHS->getType() &&
1774 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001775 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001776 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001777 "Incorrect operand type (not integer) for S/UDIV");
1778 break;
1779 case FDiv:
1780 assert(getType() == LHS->getType() &&
1781 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001782 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001783 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001784 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001785 case URem:
1786 case SRem:
1787 assert(getType() == LHS->getType() &&
1788 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001789 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001790 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001791 "Incorrect operand type (not integer) for S/UREM");
1792 break;
1793 case FRem:
1794 assert(getType() == LHS->getType() &&
1795 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001796 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001797 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001798 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001799 case Shl:
1800 case LShr:
1801 case AShr:
1802 assert(getType() == LHS->getType() &&
1803 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001804 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001805 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001806 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001807 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001808 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001809 case And: case Or:
1810 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001811 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001812 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001813 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001814 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001815 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001816 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001817 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001818 default:
1819 break;
1820 }
1821#endif
1822}
1823
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001824BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001825 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001826 Instruction *InsertBefore) {
1827 assert(S1->getType() == S2->getType() &&
1828 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001829 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001830}
1831
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001832BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001833 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001834 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001835 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001836 InsertAtEnd->getInstList().push_back(Res);
1837 return Res;
1838}
1839
Dan Gohman5476cfd2009-08-12 16:23:25 +00001840BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001841 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001842 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001843 return new BinaryOperator(Instruction::Sub,
1844 zero, Op,
1845 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001846}
1847
Dan Gohman5476cfd2009-08-12 16:23:25 +00001848BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001849 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001850 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001851 return new BinaryOperator(Instruction::Sub,
1852 zero, Op,
1853 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001854}
1855
Dan Gohman4ab44202009-12-18 02:58:50 +00001856BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1857 Instruction *InsertBefore) {
1858 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1859 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1860}
1861
1862BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1863 BasicBlock *InsertAtEnd) {
1864 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1865 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1866}
1867
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001868BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1869 Instruction *InsertBefore) {
1870 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1871 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1872}
1873
1874BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1875 BasicBlock *InsertAtEnd) {
1876 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1877 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1878}
1879
Dan Gohman5476cfd2009-08-12 16:23:25 +00001880BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001881 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001882 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001883 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001884 Op->getType(), Name, InsertBefore);
1885}
1886
Dan Gohman5476cfd2009-08-12 16:23:25 +00001887BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001888 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001889 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001890 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001891 Op->getType(), Name, InsertAtEnd);
1892}
1893
Dan Gohman5476cfd2009-08-12 16:23:25 +00001894BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001895 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001896 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001897 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001898 Op->getType(), Name, InsertBefore);
1899}
1900
Dan Gohman5476cfd2009-08-12 16:23:25 +00001901BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001902 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001903 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001904 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001905 Op->getType(), Name, InsertAtEnd);
1906}
1907
1908
1909// isConstantAllOnes - Helper function for several functions below
1910static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001911 if (const Constant *C = dyn_cast<Constant>(V))
1912 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001913 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001914}
1915
Owen Andersonbb2501b2009-07-13 22:18:28 +00001916bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001917 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1918 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001919 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1920 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001921 return false;
1922}
1923
Owen Andersonbb2501b2009-07-13 22:18:28 +00001924bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001925 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1926 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001927 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001928 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001929 return false;
1930}
1931
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001932bool BinaryOperator::isNot(const Value *V) {
1933 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1934 return (Bop->getOpcode() == Instruction::Xor &&
1935 (isConstantAllOnes(Bop->getOperand(1)) ||
1936 isConstantAllOnes(Bop->getOperand(0))));
1937 return false;
1938}
1939
Chris Lattner2c7d1772005-04-24 07:28:37 +00001940Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001941 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001942}
1943
Chris Lattner2c7d1772005-04-24 07:28:37 +00001944const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1945 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001946}
1947
Dan Gohmana5b96452009-06-04 22:49:04 +00001948Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001949 return cast<BinaryOperator>(BinOp)->getOperand(1);
1950}
1951
1952const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1953 return getFNegArgument(const_cast<Value*>(BinOp));
1954}
1955
Chris Lattner2c7d1772005-04-24 07:28:37 +00001956Value *BinaryOperator::getNotArgument(Value *BinOp) {
1957 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1958 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1959 Value *Op0 = BO->getOperand(0);
1960 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001961 if (isConstantAllOnes(Op0)) return Op1;
1962
1963 assert(isConstantAllOnes(Op1));
1964 return Op0;
1965}
1966
Chris Lattner2c7d1772005-04-24 07:28:37 +00001967const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1968 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001969}
1970
1971
1972// swapOperands - Exchange the two operands to this instruction. This
1973// instruction is safe to use on any binary instruction and does not
1974// modify the semantics of the instruction. If the instruction is
1975// order dependent (SetLT f.e.) the opcode is changed.
1976//
1977bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001978 if (!isCommutative())
1979 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001980 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001981 return false;
1982}
1983
Dan Gohman1b849082009-09-07 23:54:19 +00001984void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1985 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1986}
1987
1988void BinaryOperator::setHasNoSignedWrap(bool b) {
1989 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1990}
1991
1992void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001993 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001994}
1995
Nick Lewycky28a5f252009-09-27 21:33:04 +00001996bool BinaryOperator::hasNoUnsignedWrap() const {
1997 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1998}
1999
2000bool BinaryOperator::hasNoSignedWrap() const {
2001 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2002}
2003
2004bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002005 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002006}
2007
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002008//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002009// FPMathOperator Class
2010//===----------------------------------------------------------------------===//
2011
2012/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2013/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002014/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002015float FPMathOperator::getFPAccuracy() const {
2016 const MDNode *MD =
2017 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2018 if (!MD)
2019 return 0.0;
Duncan Sands9af62982012-04-16 19:39:33 +00002020 ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2021 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002022}
2023
2024
2025//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002026// CastInst Class
2027//===----------------------------------------------------------------------===//
2028
David Blaikie3a15e142011-12-01 08:00:17 +00002029void CastInst::anchor() {}
2030
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002031// Just determine if this cast only deals with integral->integral conversion.
2032bool CastInst::isIntegerCast() const {
2033 switch (getOpcode()) {
2034 default: return false;
2035 case Instruction::ZExt:
2036 case Instruction::SExt:
2037 case Instruction::Trunc:
2038 return true;
2039 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002040 return getOperand(0)->getType()->isIntegerTy() &&
2041 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002042 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002043}
2044
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045bool CastInst::isLosslessCast() const {
2046 // Only BitCast can be lossless, exit fast if we're not BitCast
2047 if (getOpcode() != Instruction::BitCast)
2048 return false;
2049
2050 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002051 Type* SrcTy = getOperand(0)->getType();
2052 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002053 if (SrcTy == DstTy)
2054 return true;
2055
Reid Spencer8d9336d2006-12-31 05:26:44 +00002056 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002057 if (SrcTy->isPointerTy())
2058 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002059 return false; // Other types have no identity values
2060}
2061
2062/// This function determines if the CastInst does not require any bits to be
2063/// changed in order to effect the cast. Essentially, it identifies cases where
2064/// no code gen is necessary for the cast, hence the name no-op cast. For
2065/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002066/// # bitcast i32* %x to i8*
2067/// # bitcast <2 x i32> %x to <4 x i16>
2068/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002069/// @brief Determine if the described cast is a no-op.
2070bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002071 Type *SrcTy,
2072 Type *DestTy,
2073 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002074 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002075 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002076 case Instruction::Trunc:
2077 case Instruction::ZExt:
2078 case Instruction::SExt:
2079 case Instruction::FPTrunc:
2080 case Instruction::FPExt:
2081 case Instruction::UIToFP:
2082 case Instruction::SIToFP:
2083 case Instruction::FPToUI:
2084 case Instruction::FPToSI:
2085 return false; // These always modify bits
2086 case Instruction::BitCast:
2087 return true; // BitCast never modifies bits.
2088 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002089 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002090 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002091 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002092 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002093 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 }
2095}
2096
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002097/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002098bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002099 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2100}
2101
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002102/// This function determines if a pair of casts can be eliminated and what
2103/// opcode should be used in the elimination. This assumes that there are two
2104/// instructions like this:
2105/// * %F = firstOpcode SrcTy %x to MidTy
2106/// * %S = secondOpcode MidTy %F to DstTy
2107/// The function returns a resultOpcode so these two casts can be replaced with:
2108/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2109/// If no such cast is permited, the function returns 0.
2110unsigned CastInst::isEliminableCastPair(
2111 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002112 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2113 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002114 // Define the 144 possibilities for these two cast instructions. The values
2115 // in this matrix determine what to do in a given situation and select the
2116 // case in the switch below. The rows correspond to firstOp, the columns
2117 // correspond to secondOp. In looking at the table below, keep in mind
2118 // the following cast properties:
2119 //
2120 // Size Compare Source Destination
2121 // Operator Src ? Size Type Sign Type Sign
2122 // -------- ------------ ------------------- ---------------------
2123 // TRUNC > Integer Any Integral Any
2124 // ZEXT < Integral Unsigned Integer Any
2125 // SEXT < Integral Signed Integer Any
2126 // FPTOUI n/a FloatPt n/a Integral Unsigned
2127 // FPTOSI n/a FloatPt n/a Integral Signed
2128 // UITOFP n/a Integral Unsigned FloatPt n/a
2129 // SITOFP n/a Integral Signed FloatPt n/a
2130 // FPTRUNC > FloatPt n/a FloatPt n/a
2131 // FPEXT < FloatPt n/a FloatPt n/a
2132 // PTRTOINT n/a Pointer n/a Integral Unsigned
2133 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002134 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002135 //
2136 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002137 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2138 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002139 // of the produced value (we no longer know the top-part is all zeros).
2140 // Further this conversion is often much more expensive for typical hardware,
2141 // and causes issues when building libgcc. We disallow fptosi+sext for the
2142 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002143 const unsigned numCastOps =
2144 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2145 static const uint8_t CastResults[numCastOps][numCastOps] = {
2146 // T F F U S F F P I B -+
2147 // R Z S P P I I T P 2 N T |
2148 // U E E 2 2 2 2 R E I T C +- secondOp
2149 // N X X U S F F N X N 2 V |
2150 // C T T I I P P C T T P T -+
2151 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2152 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2153 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002154 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2155 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002156 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2157 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2158 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2159 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2160 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2161 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2162 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2163 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002164
2165 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002166 // merging. However, bitcast of A->B->A are allowed.
2167 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2168 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2169 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2170
2171 // Check if any of the bitcasts convert scalars<->vectors.
2172 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2173 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2174 // Unless we are bitcasing to the original type, disallow optimizations.
2175 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176
2177 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2178 [secondOp-Instruction::CastOpsBegin];
2179 switch (ElimCase) {
2180 case 0:
2181 // categorically disallowed
2182 return 0;
2183 case 1:
2184 // allowed, use first cast's opcode
2185 return firstOp;
2186 case 2:
2187 // allowed, use second cast's opcode
2188 return secondOp;
2189 case 3:
2190 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002191 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002192 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002193 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002194 return firstOp;
2195 return 0;
2196 case 4:
2197 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002198 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002199 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200 return firstOp;
2201 return 0;
2202 case 5:
2203 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002204 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002205 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002206 return secondOp;
2207 return 0;
2208 case 6:
2209 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002210 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002211 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002212 return secondOp;
2213 return 0;
2214 case 7: {
2215 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Duncan Sandse2395dc2012-10-30 16:03:32 +00002216 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002217 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002218 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002219 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002220 if (MidSize >= PtrSize)
2221 return Instruction::BitCast;
2222 return 0;
2223 }
2224 case 8: {
2225 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2226 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2227 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002228 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2229 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002230 if (SrcSize == DstSize)
2231 return Instruction::BitCast;
2232 else if (SrcSize < DstSize)
2233 return firstOp;
2234 return secondOp;
2235 }
2236 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2237 return Instruction::ZExt;
2238 case 10:
2239 // fpext followed by ftrunc is allowed if the bit size returned to is
2240 // the same as the original, in which case its just a bitcast
2241 if (SrcTy == DstTy)
2242 return Instruction::BitCast;
2243 return 0; // If the types are not the same we can't eliminate it.
2244 case 11:
2245 // bitcast followed by ptrtoint is allowed as long as the bitcast
2246 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002247 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248 return secondOp;
2249 return 0;
2250 case 12:
2251 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002252 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002253 return firstOp;
2254 return 0;
2255 case 13: {
2256 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002257 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002258 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002259 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002260 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2261 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002262 if (SrcSize <= PtrSize && SrcSize == DstSize)
2263 return Instruction::BitCast;
2264 return 0;
2265 }
2266 case 99:
2267 // cast combination can't happen (error in input). This is for all cases
2268 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002269 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270 default:
Craig Topperc514b542012-02-05 22:14:15 +00002271 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002272 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002273}
2274
Chris Lattner229907c2011-07-18 04:54:35 +00002275CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002276 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002277 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002278 // Construct and return the appropriate CastInst subclass
2279 switch (op) {
2280 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2281 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2282 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2283 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2284 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2285 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2286 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2287 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2288 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2289 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2290 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2291 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
Craig Topperc514b542012-02-05 22:14:15 +00002292 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294}
2295
Chris Lattner229907c2011-07-18 04:54:35 +00002296CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002297 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002298 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 // Construct and return the appropriate CastInst subclass
2300 switch (op) {
2301 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2302 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2303 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2304 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2305 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2306 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2307 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2308 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2309 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2310 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2311 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2312 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
Craig Topperc514b542012-02-05 22:14:15 +00002313 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315}
2316
Chris Lattner229907c2011-07-18 04:54:35 +00002317CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002318 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002319 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002320 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002321 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2322 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002323}
2324
Chris Lattner229907c2011-07-18 04:54:35 +00002325CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002326 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002327 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002328 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002329 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2330 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002331}
2332
Chris Lattner229907c2011-07-18 04:54:35 +00002333CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002334 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002335 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002336 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002337 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2338 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002339}
2340
Chris Lattner229907c2011-07-18 04:54:35 +00002341CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002342 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002343 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002344 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002345 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2346 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002347}
2348
Chris Lattner229907c2011-07-18 04:54:35 +00002349CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002350 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002351 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002352 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002353 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2354 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002355}
2356
Chris Lattner229907c2011-07-18 04:54:35 +00002357CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002358 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002359 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002360 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002361 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2362 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002363}
2364
Chris Lattner229907c2011-07-18 04:54:35 +00002365CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002366 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002367 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002368 assert(S->getType()->isPointerTy() && "Invalid cast");
2369 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002370 "Invalid cast");
2371
Duncan Sands9dff9be2010-02-15 16:12:20 +00002372 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002373 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2374 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002375}
2376
2377/// @brief Create a BitCast or a PtrToInt cast instruction
Chris Lattner229907c2011-07-18 04:54:35 +00002378CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002379 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002380 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002381 assert(S->getType()->isPointerTy() && "Invalid cast");
2382 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002383 "Invalid cast");
2384
Duncan Sands9dff9be2010-02-15 16:12:20 +00002385 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002386 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2387 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002388}
2389
Chris Lattner229907c2011-07-18 04:54:35 +00002390CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002391 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002392 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002393 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002394 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002395 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2396 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002397 Instruction::CastOps opcode =
2398 (SrcBits == DstBits ? Instruction::BitCast :
2399 (SrcBits > DstBits ? Instruction::Trunc :
2400 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002401 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002402}
2403
Chris Lattner229907c2011-07-18 04:54:35 +00002404CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002405 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002406 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002407 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002408 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002409 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2410 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002411 Instruction::CastOps opcode =
2412 (SrcBits == DstBits ? Instruction::BitCast :
2413 (SrcBits > DstBits ? Instruction::Trunc :
2414 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002415 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002416}
2417
Chris Lattner229907c2011-07-18 04:54:35 +00002418CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002419 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002420 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002421 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002422 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002423 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2424 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002425 Instruction::CastOps opcode =
2426 (SrcBits == DstBits ? Instruction::BitCast :
2427 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002428 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002429}
2430
Chris Lattner229907c2011-07-18 04:54:35 +00002431CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002432 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002433 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002434 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002435 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002436 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2437 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002438 Instruction::CastOps opcode =
2439 (SrcBits == DstBits ? Instruction::BitCast :
2440 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002441 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002442}
2443
Duncan Sands55e50902008-01-06 10:12:28 +00002444// Check whether it is valid to call getCastOpcode for these types.
2445// This routine must be kept in sync with getCastOpcode.
Chris Lattner229907c2011-07-18 04:54:35 +00002446bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
Duncan Sands55e50902008-01-06 10:12:28 +00002447 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2448 return false;
2449
2450 if (SrcTy == DestTy)
2451 return true;
2452
Chris Lattner229907c2011-07-18 04:54:35 +00002453 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2454 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002455 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2456 // An element by element cast. Valid if casting the elements is valid.
2457 SrcTy = SrcVecTy->getElementType();
2458 DestTy = DestVecTy->getElementType();
2459 }
2460
Duncan Sands55e50902008-01-06 10:12:28 +00002461 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002462 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2463 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002464
2465 // Run through the possibilities ...
Duncan Sands27bd0df2011-05-18 10:59:25 +00002466 if (DestTy->isIntegerTy()) { // Casting to integral
2467 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002468 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002469 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002470 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002471 } else if (SrcTy->isVectorTy()) { // Casting from vector
2472 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002473 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002474 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002475 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002476 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2477 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002478 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002479 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002480 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002481 } else if (SrcTy->isVectorTy()) { // Casting from vector
2482 return DestBits == SrcBits;
Gabor Greif697e94c2008-05-15 10:04:30 +00002483 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002484 return false;
2485 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002486 } else if (DestTy->isVectorTy()) { // Casting to vector
2487 return DestBits == SrcBits;
Duncan Sands19d0b472010-02-16 11:11:14 +00002488 } else if (DestTy->isPointerTy()) { // Casting to pointer
Duncan Sands27bd0df2011-05-18 10:59:25 +00002489 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002490 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002491 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002492 return true;
Duncan Sands27bd0df2011-05-18 10:59:25 +00002493 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002494 return false;
2495 }
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002496 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002497 if (SrcTy->isVectorTy()) {
2498 return DestBits == SrcBits; // 64-bit vector to MMX
Duncan Sands2d3cdd62011-04-01 03:34:54 +00002499 } else {
2500 return false;
2501 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002502 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002503 return false;
2504 }
2505}
2506
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507// Provide a way to get a "cast" where the cast opcode is inferred from the
2508// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002509// logic in the castIsValid function below. This axiom should hold:
2510// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2511// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002513// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002515CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002516 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2517 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518
Duncan Sands55e50902008-01-06 10:12:28 +00002519 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2520 "Only first class types are castable!");
2521
Duncan Sandsa8514532011-05-18 07:13:41 +00002522 if (SrcTy == DestTy)
2523 return BitCast;
2524
Chris Lattner229907c2011-07-18 04:54:35 +00002525 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2526 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002527 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2528 // An element by element cast. Find the appropriate opcode based on the
2529 // element types.
2530 SrcTy = SrcVecTy->getElementType();
2531 DestTy = DestVecTy->getElementType();
2532 }
2533
2534 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002535 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2536 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002537
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002539 if (DestTy->isIntegerTy()) { // Casting to integral
2540 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 if (DestBits < SrcBits)
2542 return Trunc; // int -> smaller int
2543 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002544 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545 return SExt; // signed -> SEXT
2546 else
2547 return ZExt; // unsigned -> ZEXT
2548 } else {
2549 return BitCast; // Same size, No-op cast
2550 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002551 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002552 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553 return FPToSI; // FP -> sint
2554 else
2555 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002556 } else if (SrcTy->isVectorTy()) {
2557 assert(DestBits == SrcBits &&
2558 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559 return BitCast; // Same size, no-op cast
2560 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002561 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562 "Casting from a value that is not first-class type");
2563 return PtrToInt; // ptr -> int
2564 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002565 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2566 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002567 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568 return SIToFP; // sint -> FP
2569 else
2570 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002571 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572 if (DestBits < SrcBits) {
2573 return FPTrunc; // FP -> smaller FP
2574 } else if (DestBits > SrcBits) {
2575 return FPExt; // FP -> larger FP
2576 } else {
2577 return BitCast; // same size, no-op cast
2578 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002579 } else if (SrcTy->isVectorTy()) {
2580 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002581 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002582 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002584 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002585 } else if (DestTy->isVectorTy()) {
2586 assert(DestBits == SrcBits &&
2587 "Illegal cast to vector (wrong type or size)");
2588 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002589 } else if (DestTy->isPointerTy()) {
2590 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002592 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002595 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002596 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002597 if (SrcTy->isVectorTy()) {
2598 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002599 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002600 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002601 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002603 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604}
2605
2606//===----------------------------------------------------------------------===//
2607// CastInst SubClass Constructors
2608//===----------------------------------------------------------------------===//
2609
2610/// Check that the construction parameters for a CastInst are correct. This
2611/// could be broken out into the separate constructors but it is useful to have
2612/// it in one place and to eliminate the redundant code for getting the sizes
2613/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002614bool
Chris Lattner229907c2011-07-18 04:54:35 +00002615CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616
2617 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002618 Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002619 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2620 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621 return false;
2622
2623 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002624 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2625 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626
Duncan Sands7f646562011-05-18 09:21:57 +00002627 // If these are vector types, get the lengths of the vectors (using zero for
2628 // scalar types means that checking that vector lengths match also checks that
2629 // scalars are not being converted to vectors or vectors to scalars).
2630 unsigned SrcLength = SrcTy->isVectorTy() ?
2631 cast<VectorType>(SrcTy)->getNumElements() : 0;
2632 unsigned DstLength = DstTy->isVectorTy() ?
2633 cast<VectorType>(DstTy)->getNumElements() : 0;
2634
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635 // Switch on the opcode provided
2636 switch (op) {
2637 default: return false; // This is an input error
2638 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002639 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2640 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002641 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002642 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2643 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002644 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002645 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2646 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002648 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2649 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002650 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002651 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2652 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002655 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2656 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002658 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002659 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2660 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002662 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002663 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002664 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2665 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2666 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002667 return SrcTy->getScalarType()->isPointerTy() &&
2668 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002669 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002670 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002671 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002672 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2673 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2674 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002675 return SrcTy->getScalarType()->isIntegerTy() &&
2676 DstTy->getScalarType()->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677 case Instruction::BitCast:
2678 // BitCast implies a no-op cast of type only. No bits change.
2679 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002680 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002681 return false;
2682
Duncan Sands55e50902008-01-06 10:12:28 +00002683 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002684 // these cases, the cast is okay if the source and destination bit widths
2685 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002686 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002687 }
2688}
2689
2690TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002691 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002692) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002693 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002694}
2695
2696TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002697 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002698) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002699 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002700}
2701
2702ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002703 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002704) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002705 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002706}
2707
2708ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002709 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002710) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002711 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002712}
2713SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002714 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002715) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002716 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002717}
2718
Jeff Cohencc08c832006-12-02 02:22:01 +00002719SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002720 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002721) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002722 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002723}
2724
2725FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002726 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002727) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002728 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002729}
2730
2731FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002732 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002733) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002734 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002735}
2736
2737FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002738 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002740 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002741}
2742
2743FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002744 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002745) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002746 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002747}
2748
2749UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002750 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002751) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002752 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002753}
2754
2755UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002756 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002757) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002758 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002759}
2760
2761SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002762 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002763) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002764 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002765}
2766
2767SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002768 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002769) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002770 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002771}
2772
2773FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002774 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002775) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002776 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002777}
2778
2779FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002780 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002781) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002782 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002783}
2784
2785FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002786 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002787) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002788 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002789}
2790
2791FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002792 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002793) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002794 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002795}
2796
2797PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002798 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002799) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002800 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002801}
2802
2803PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002804 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002805) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002806 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002807}
2808
2809IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002810 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002811) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002812 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002813}
2814
2815IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002816 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002817) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002818 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002819}
2820
2821BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002822 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002823) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002824 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002825}
2826
2827BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002828 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002829) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002830 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002831}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002832
2833//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002834// CmpInst Classes
2835//===----------------------------------------------------------------------===//
2836
Craig Topper3186c012012-09-23 02:12:10 +00002837void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002838
Chris Lattner229907c2011-07-18 04:54:35 +00002839CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002840 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002841 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002842 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002843 OperandTraits<CmpInst>::op_begin(this),
2844 OperandTraits<CmpInst>::operands(this),
2845 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002846 Op<0>() = LHS;
2847 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002848 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002849 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002850}
Gabor Greiff6caff662008-05-10 08:32:32 +00002851
Chris Lattner229907c2011-07-18 04:54:35 +00002852CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002853 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002854 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002855 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002856 OperandTraits<CmpInst>::op_begin(this),
2857 OperandTraits<CmpInst>::operands(this),
2858 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002859 Op<0>() = LHS;
2860 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002861 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002862 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002863}
2864
2865CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002866CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002867 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002868 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002869 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002870 if (InsertBefore)
2871 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2872 S1, S2, Name);
2873 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002874 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002875 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002876 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002877
2878 if (InsertBefore)
2879 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2880 S1, S2, Name);
2881 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002882 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002883 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002884}
2885
2886CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002887CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002888 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002889 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002890 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2891 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002892 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002893 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2894 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002895}
2896
2897void CmpInst::swapOperands() {
2898 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2899 IC->swapOperands();
2900 else
2901 cast<FCmpInst>(this)->swapOperands();
2902}
2903
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002904bool CmpInst::isCommutative() const {
2905 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002906 return IC->isCommutative();
2907 return cast<FCmpInst>(this)->isCommutative();
2908}
2909
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002910bool CmpInst::isEquality() const {
2911 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002912 return IC->isEquality();
2913 return cast<FCmpInst>(this)->isEquality();
2914}
2915
2916
Dan Gohman4e724382008-05-31 02:47:54 +00002917CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002918 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002919 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002920 case ICMP_EQ: return ICMP_NE;
2921 case ICMP_NE: return ICMP_EQ;
2922 case ICMP_UGT: return ICMP_ULE;
2923 case ICMP_ULT: return ICMP_UGE;
2924 case ICMP_UGE: return ICMP_ULT;
2925 case ICMP_ULE: return ICMP_UGT;
2926 case ICMP_SGT: return ICMP_SLE;
2927 case ICMP_SLT: return ICMP_SGE;
2928 case ICMP_SGE: return ICMP_SLT;
2929 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002930
Dan Gohman4e724382008-05-31 02:47:54 +00002931 case FCMP_OEQ: return FCMP_UNE;
2932 case FCMP_ONE: return FCMP_UEQ;
2933 case FCMP_OGT: return FCMP_ULE;
2934 case FCMP_OLT: return FCMP_UGE;
2935 case FCMP_OGE: return FCMP_ULT;
2936 case FCMP_OLE: return FCMP_UGT;
2937 case FCMP_UEQ: return FCMP_ONE;
2938 case FCMP_UNE: return FCMP_OEQ;
2939 case FCMP_UGT: return FCMP_OLE;
2940 case FCMP_ULT: return FCMP_OGE;
2941 case FCMP_UGE: return FCMP_OLT;
2942 case FCMP_ULE: return FCMP_OGT;
2943 case FCMP_ORD: return FCMP_UNO;
2944 case FCMP_UNO: return FCMP_ORD;
2945 case FCMP_TRUE: return FCMP_FALSE;
2946 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002947 }
2948}
2949
Reid Spencer266e42b2006-12-23 06:05:41 +00002950ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2951 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002952 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002953 case ICMP_EQ: case ICMP_NE:
2954 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2955 return pred;
2956 case ICMP_UGT: return ICMP_SGT;
2957 case ICMP_ULT: return ICMP_SLT;
2958 case ICMP_UGE: return ICMP_SGE;
2959 case ICMP_ULE: return ICMP_SLE;
2960 }
2961}
2962
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002963ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2964 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00002965 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002966 case ICMP_EQ: case ICMP_NE:
2967 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2968 return pred;
2969 case ICMP_SGT: return ICMP_UGT;
2970 case ICMP_SLT: return ICMP_ULT;
2971 case ICMP_SGE: return ICMP_UGE;
2972 case ICMP_SLE: return ICMP_ULE;
2973 }
2974}
2975
Reid Spencer0286bc12007-02-28 22:00:54 +00002976/// Initialize a set of values that all satisfy the condition with C.
2977///
2978ConstantRange
2979ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2980 APInt Lower(C);
2981 APInt Upper(C);
2982 uint32_t BitWidth = C.getBitWidth();
2983 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002984 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002985 case ICmpInst::ICMP_EQ: Upper++; break;
2986 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002987 case ICmpInst::ICMP_ULT:
2988 Lower = APInt::getMinValue(BitWidth);
2989 // Check for an empty-set condition.
2990 if (Lower == Upper)
2991 return ConstantRange(BitWidth, /*isFullSet=*/false);
2992 break;
2993 case ICmpInst::ICMP_SLT:
2994 Lower = APInt::getSignedMinValue(BitWidth);
2995 // Check for an empty-set condition.
2996 if (Lower == Upper)
2997 return ConstantRange(BitWidth, /*isFullSet=*/false);
2998 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002999 case ICmpInst::ICMP_UGT:
3000 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003001 // Check for an empty-set condition.
3002 if (Lower == Upper)
3003 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003004 break;
3005 case ICmpInst::ICMP_SGT:
3006 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003007 // Check for an empty-set condition.
3008 if (Lower == Upper)
3009 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003010 break;
3011 case ICmpInst::ICMP_ULE:
3012 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003013 // Check for a full-set condition.
3014 if (Lower == Upper)
3015 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003016 break;
3017 case ICmpInst::ICMP_SLE:
3018 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00003019 // Check for a full-set condition.
3020 if (Lower == Upper)
3021 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003022 break;
3023 case ICmpInst::ICMP_UGE:
3024 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003025 // Check for a full-set condition.
3026 if (Lower == Upper)
3027 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003028 break;
3029 case ICmpInst::ICMP_SGE:
3030 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003031 // Check for a full-set condition.
3032 if (Lower == Upper)
3033 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003034 break;
3035 }
3036 return ConstantRange(Lower, Upper);
3037}
3038
Dan Gohman4e724382008-05-31 02:47:54 +00003039CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003040 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003041 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003042 case ICMP_EQ: case ICMP_NE:
3043 return pred;
3044 case ICMP_SGT: return ICMP_SLT;
3045 case ICMP_SLT: return ICMP_SGT;
3046 case ICMP_SGE: return ICMP_SLE;
3047 case ICMP_SLE: return ICMP_SGE;
3048 case ICMP_UGT: return ICMP_ULT;
3049 case ICMP_ULT: return ICMP_UGT;
3050 case ICMP_UGE: return ICMP_ULE;
3051 case ICMP_ULE: return ICMP_UGE;
3052
Reid Spencerd9436b62006-11-20 01:22:35 +00003053 case FCMP_FALSE: case FCMP_TRUE:
3054 case FCMP_OEQ: case FCMP_ONE:
3055 case FCMP_UEQ: case FCMP_UNE:
3056 case FCMP_ORD: case FCMP_UNO:
3057 return pred;
3058 case FCMP_OGT: return FCMP_OLT;
3059 case FCMP_OLT: return FCMP_OGT;
3060 case FCMP_OGE: return FCMP_OLE;
3061 case FCMP_OLE: return FCMP_OGE;
3062 case FCMP_UGT: return FCMP_ULT;
3063 case FCMP_ULT: return FCMP_UGT;
3064 case FCMP_UGE: return FCMP_ULE;
3065 case FCMP_ULE: return FCMP_UGE;
3066 }
3067}
3068
Reid Spencer266e42b2006-12-23 06:05:41 +00003069bool CmpInst::isUnsigned(unsigned short predicate) {
3070 switch (predicate) {
3071 default: return false;
3072 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3073 case ICmpInst::ICMP_UGE: return true;
3074 }
3075}
3076
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003077bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003078 switch (predicate) {
3079 default: return false;
3080 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3081 case ICmpInst::ICMP_SGE: return true;
3082 }
3083}
3084
3085bool CmpInst::isOrdered(unsigned short predicate) {
3086 switch (predicate) {
3087 default: return false;
3088 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3089 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3090 case FCmpInst::FCMP_ORD: return true;
3091 }
3092}
3093
3094bool CmpInst::isUnordered(unsigned short predicate) {
3095 switch (predicate) {
3096 default: return false;
3097 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3098 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3099 case FCmpInst::FCMP_UNO: return true;
3100 }
3101}
3102
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003103bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3104 switch(predicate) {
3105 default: return false;
3106 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3107 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3108 }
3109}
3110
3111bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3112 switch(predicate) {
3113 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3114 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3115 default: return false;
3116 }
3117}
3118
3119
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003120//===----------------------------------------------------------------------===//
3121// SwitchInst Implementation
3122//===----------------------------------------------------------------------===//
3123
Chris Lattnerbaf00152010-11-17 05:41:46 +00003124void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3125 assert(Value && Default && NumReserved);
3126 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003127 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003128 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003129
Gabor Greif2d3024d2008-05-26 21:33:52 +00003130 OperandList[0] = Value;
3131 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003132}
3133
Chris Lattner2195fc42007-02-24 00:55:48 +00003134/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3135/// switch on and a default destination. The number of additional cases can
3136/// be specified here to make memory allocation more efficient. This
3137/// constructor can also autoinsert before another instruction.
3138SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3139 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003140 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3141 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003142 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003143}
3144
3145/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3146/// switch on and a default destination. The number of additional cases can
3147/// be specified here to make memory allocation more efficient. This
3148/// constructor also autoinserts at the end of the specified BasicBlock.
3149SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3150 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003151 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3152 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003153 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003154}
3155
Misha Brukmanb1c93172005-04-21 23:48:37 +00003156SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00003157 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3158 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3159 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003160 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003161 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003162 OL[i] = InOL[i];
3163 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003164 }
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003165 TheSubsets = SI.TheSubsets;
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003166 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003167}
3168
Gordon Henriksen14a55692007-12-10 02:14:30 +00003169SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003170 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003171}
3172
3173
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003174/// addCase - Add an entry to the switch instruction...
3175///
Chris Lattner47ac1872005-02-24 05:32:09 +00003176void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003177 IntegersSubsetToBB Mapping;
Stepan Dyatkovskiye3e19cb2012-05-28 12:39:09 +00003178
3179 // FIXME: Currently we work with ConstantInt based cases.
3180 // So inititalize IntItem container directly from ConstantInt.
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003181 Mapping.add(IntItem::fromConstantInt(OnVal));
3182 IntegersSubset CaseRanges = Mapping.getCase();
3183 addCase(CaseRanges, Dest);
Stepan Dyatkovskiy0beab5e2012-05-12 10:48:17 +00003184}
3185
Stepan Dyatkovskiy58107dd2012-05-29 12:26:47 +00003186void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003187 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003188 unsigned OpNo = NumOperands;
3189 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003190 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003191 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003192 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003193 NumOperands = OpNo+2;
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003194
3195 SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3196
3197 CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3198 Case.updateCaseValueOperand(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003199 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003200}
3201
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003202/// removeCase - This method removes the specified case and its successor
3203/// from the switch instruction.
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003204void SwitchInst::removeCase(CaseIt& i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003205 unsigned idx = i.getCaseIndex();
3206
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003207 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003208
3209 unsigned NumOps = getNumOperands();
3210 Use *OL = OperandList;
3211
Jay Foad14277722011-02-01 09:22:34 +00003212 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003213 if (2 + (idx + 1) * 2 != NumOps) {
3214 OL[2 + idx * 2] = OL[NumOps - 2];
3215 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003216 }
3217
3218 // Nuke the last value.
3219 OL[NumOps-2].set(0);
3220 OL[NumOps-2+1].set(0);
Stepan Dyatkovskiya6c8cc32012-06-22 14:53:30 +00003221
3222 // Do the same with TheCases collection:
3223 if (i.SubsetIt != --TheSubsets.end()) {
3224 *i.SubsetIt = TheSubsets.back();
3225 TheSubsets.pop_back();
3226 } else {
3227 TheSubsets.pop_back();
3228 i.SubsetIt = TheSubsets.end();
3229 }
3230
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003231 NumOperands = NumOps-2;
3232}
3233
Jay Foade98f29d2011-04-01 08:00:58 +00003234/// growOperands - grow operands - This grows the operand list in response
3235/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003236///
Jay Foade98f29d2011-04-01 08:00:58 +00003237void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003238 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003239 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003240
3241 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003242 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003243 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003244 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003245 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003246 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003247 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003248 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003249}
3250
3251
3252BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3253 return getSuccessor(idx);
3254}
3255unsigned SwitchInst::getNumSuccessorsV() const {
3256 return getNumSuccessors();
3257}
3258void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3259 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003260}
Chris Lattnerf22be932004-10-15 23:52:53 +00003261
Chris Lattner3ed871f2009-10-27 19:13:16 +00003262//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003263// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003264//===----------------------------------------------------------------------===//
3265
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003266void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003267 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003268 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003269 ReservedSpace = 1+NumDests;
3270 NumOperands = 1;
3271 OperandList = allocHungoffUses(ReservedSpace);
3272
3273 OperandList[0] = Address;
3274}
3275
3276
Jay Foade98f29d2011-04-01 08:00:58 +00003277/// growOperands - grow operands - This grows the operand list in response
3278/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003279///
Jay Foade98f29d2011-04-01 08:00:58 +00003280void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003281 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003282 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003283
3284 ReservedSpace = NumOps;
3285 Use *NewOps = allocHungoffUses(NumOps);
3286 Use *OldOps = OperandList;
3287 for (unsigned i = 0; i != e; ++i)
3288 NewOps[i] = OldOps[i];
3289 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003290 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003291}
3292
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003293IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3294 Instruction *InsertBefore)
3295: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003296 0, 0, InsertBefore) {
3297 init(Address, NumCases);
3298}
3299
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003300IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3301 BasicBlock *InsertAtEnd)
3302: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003303 0, 0, InsertAtEnd) {
3304 init(Address, NumCases);
3305}
3306
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003307IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3308 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003309 allocHungoffUses(IBI.getNumOperands()),
3310 IBI.getNumOperands()) {
3311 Use *OL = OperandList, *InOL = IBI.OperandList;
3312 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3313 OL[i] = InOL[i];
3314 SubclassOptionalData = IBI.SubclassOptionalData;
3315}
3316
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003317IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003318 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003319}
3320
3321/// addDestination - Add a destination.
3322///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003323void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003324 unsigned OpNo = NumOperands;
3325 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003326 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003327 // Initialize some new operands.
3328 assert(OpNo < ReservedSpace && "Growing didn't work!");
3329 NumOperands = OpNo+1;
3330 OperandList[OpNo] = DestBB;
3331}
3332
3333/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003334/// indirectbr instruction.
3335void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003336 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3337
3338 unsigned NumOps = getNumOperands();
3339 Use *OL = OperandList;
3340
3341 // Replace this value with the last one.
3342 OL[idx+1] = OL[NumOps-1];
3343
3344 // Nuke the last value.
3345 OL[NumOps-1].set(0);
3346 NumOperands = NumOps-1;
3347}
3348
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003349BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003350 return getSuccessor(idx);
3351}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003352unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003353 return getNumSuccessors();
3354}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003355void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003356 setSuccessor(idx, B);
3357}
3358
3359//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003360// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003361//===----------------------------------------------------------------------===//
3362
Chris Lattnerf22be932004-10-15 23:52:53 +00003363// Define these methods here so vtables don't get emitted into every translation
3364// unit that uses these classes.
3365
Devang Patel11cf3f42009-10-27 22:16:29 +00003366GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3367 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003368}
3369
Devang Patel11cf3f42009-10-27 22:16:29 +00003370BinaryOperator *BinaryOperator::clone_impl() const {
3371 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003372}
3373
Devang Patel11cf3f42009-10-27 22:16:29 +00003374FCmpInst* FCmpInst::clone_impl() const {
3375 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003376}
3377
Devang Patel11cf3f42009-10-27 22:16:29 +00003378ICmpInst* ICmpInst::clone_impl() const {
3379 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003380}
3381
Devang Patel11cf3f42009-10-27 22:16:29 +00003382ExtractValueInst *ExtractValueInst::clone_impl() const {
3383 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003384}
3385
Devang Patel11cf3f42009-10-27 22:16:29 +00003386InsertValueInst *InsertValueInst::clone_impl() const {
3387 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003388}
3389
Devang Patel11cf3f42009-10-27 22:16:29 +00003390AllocaInst *AllocaInst::clone_impl() const {
3391 return new AllocaInst(getAllocatedType(),
3392 (Value*)getOperand(0),
3393 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003394}
3395
Devang Patel11cf3f42009-10-27 22:16:29 +00003396LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003397 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3398 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003399}
3400
Devang Patel11cf3f42009-10-27 22:16:29 +00003401StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003402 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003403 getAlignment(), getOrdering(), getSynchScope());
3404
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003405}
3406
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003407AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3408 AtomicCmpXchgInst *Result =
3409 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3410 getOrdering(), getSynchScope());
3411 Result->setVolatile(isVolatile());
3412 return Result;
3413}
3414
3415AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3416 AtomicRMWInst *Result =
3417 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3418 getOrdering(), getSynchScope());
3419 Result->setVolatile(isVolatile());
3420 return Result;
3421}
3422
Eli Friedmanfee02c62011-07-25 23:16:38 +00003423FenceInst *FenceInst::clone_impl() const {
3424 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3425}
3426
Devang Patel11cf3f42009-10-27 22:16:29 +00003427TruncInst *TruncInst::clone_impl() const {
3428 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003429}
3430
Devang Patel11cf3f42009-10-27 22:16:29 +00003431ZExtInst *ZExtInst::clone_impl() const {
3432 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003433}
3434
Devang Patel11cf3f42009-10-27 22:16:29 +00003435SExtInst *SExtInst::clone_impl() const {
3436 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003437}
3438
Devang Patel11cf3f42009-10-27 22:16:29 +00003439FPTruncInst *FPTruncInst::clone_impl() const {
3440 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003441}
3442
Devang Patel11cf3f42009-10-27 22:16:29 +00003443FPExtInst *FPExtInst::clone_impl() const {
3444 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003445}
3446
Devang Patel11cf3f42009-10-27 22:16:29 +00003447UIToFPInst *UIToFPInst::clone_impl() const {
3448 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003449}
3450
Devang Patel11cf3f42009-10-27 22:16:29 +00003451SIToFPInst *SIToFPInst::clone_impl() const {
3452 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003453}
3454
Devang Patel11cf3f42009-10-27 22:16:29 +00003455FPToUIInst *FPToUIInst::clone_impl() const {
3456 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003457}
3458
Devang Patel11cf3f42009-10-27 22:16:29 +00003459FPToSIInst *FPToSIInst::clone_impl() const {
3460 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003461}
3462
Devang Patel11cf3f42009-10-27 22:16:29 +00003463PtrToIntInst *PtrToIntInst::clone_impl() const {
3464 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003465}
3466
Devang Patel11cf3f42009-10-27 22:16:29 +00003467IntToPtrInst *IntToPtrInst::clone_impl() const {
3468 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003469}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003470
Devang Patel11cf3f42009-10-27 22:16:29 +00003471BitCastInst *BitCastInst::clone_impl() const {
3472 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003473}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003474
Devang Patel11cf3f42009-10-27 22:16:29 +00003475CallInst *CallInst::clone_impl() const {
3476 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003477}
3478
Devang Patel11cf3f42009-10-27 22:16:29 +00003479SelectInst *SelectInst::clone_impl() const {
3480 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003481}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003482
Devang Patel11cf3f42009-10-27 22:16:29 +00003483VAArgInst *VAArgInst::clone_impl() const {
3484 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003485}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003486
Devang Patel11cf3f42009-10-27 22:16:29 +00003487ExtractElementInst *ExtractElementInst::clone_impl() const {
3488 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003489}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003490
Devang Patel11cf3f42009-10-27 22:16:29 +00003491InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003492 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003493}
3494
Devang Patel11cf3f42009-10-27 22:16:29 +00003495ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003496 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003497}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003498
Devang Patel11cf3f42009-10-27 22:16:29 +00003499PHINode *PHINode::clone_impl() const {
3500 return new PHINode(*this);
3501}
3502
Bill Wendlingfae14752011-08-12 20:24:12 +00003503LandingPadInst *LandingPadInst::clone_impl() const {
3504 return new LandingPadInst(*this);
3505}
3506
Devang Patel11cf3f42009-10-27 22:16:29 +00003507ReturnInst *ReturnInst::clone_impl() const {
3508 return new(getNumOperands()) ReturnInst(*this);
3509}
3510
3511BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003512 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003513}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003514
Devang Patel11cf3f42009-10-27 22:16:29 +00003515SwitchInst *SwitchInst::clone_impl() const {
3516 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003517}
3518
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003519IndirectBrInst *IndirectBrInst::clone_impl() const {
3520 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003521}
3522
3523
Devang Patel11cf3f42009-10-27 22:16:29 +00003524InvokeInst *InvokeInst::clone_impl() const {
3525 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003526}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003527
Bill Wendlingf891bf82011-07-31 06:30:59 +00003528ResumeInst *ResumeInst::clone_impl() const {
3529 return new(1) ResumeInst(*this);
3530}
3531
Devang Patel11cf3f42009-10-27 22:16:29 +00003532UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003533 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003534 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003535}