blob: 54f2aabe67ddc1218a8087135ee8776fdb99bdd1 [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 Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
65
Chris Lattner229907c2011-07-18 04:54:35 +000066 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000067 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000068 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000069 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000070 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000071 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000072 return "selected values for vector select must be vectors";
73 if (ET->getNumElements() != VT->getNumElements())
74 return "vector select requires selected vectors to have "
75 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000076 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000077 return "select condition must be i1 or <n x i1>";
78 }
Craig Topperc6207612014-04-09 06:08:46 +000079 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000080}
81
82
83//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000084// PHINode Class
85//===----------------------------------------------------------------------===//
86
87PHINode::PHINode(const PHINode &PN)
88 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000089 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000090 ReservedSpace(PN.getNumOperands()) {
Jay Foad61ea0e42011-06-23 09:09:15 +000091 std::copy(PN.op_begin(), PN.op_end(), op_begin());
92 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000093 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000094}
95
Gordon Henriksen14a55692007-12-10 02:14:30 +000096PHINode::~PHINode() {
Jay Foadbbb91f22011-01-16 15:30:52 +000097 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +000098}
99
Jay Foad61ea0e42011-06-23 09:09:15 +0000100Use *PHINode::allocHungoffUses(unsigned N) const {
101 // Allocate the array of Uses of the incoming values, followed by a pointer
102 // (with bottom bit set) to the User, followed by the array of pointers to
103 // the incoming basic blocks.
104 size_t size = N * sizeof(Use) + sizeof(Use::UserRef)
105 + N * sizeof(BasicBlock*);
106 Use *Begin = static_cast<Use*>(::operator new(size));
107 Use *End = Begin + N;
108 (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1);
109 return Use::initTags(Begin, End);
110}
111
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000112// removeIncomingValue - Remove an incoming value. This is useful if a
113// predecessor basic block is deleted.
114Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000115 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000116
117 // Move everything after this operand down.
118 //
119 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000120 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000121 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000122 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
123 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000124
125 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000126 Op<-1>().set(nullptr);
Jay Foad61ea0e42011-06-23 09:09:15 +0000127 --NumOperands;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128
129 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000130 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000132 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000133 eraseFromParent();
134 }
135 return Removed;
136}
137
Jay Foade98f29d2011-04-01 08:00:58 +0000138/// growOperands - grow operands - This grows the operand list in response
139/// to a push_back style of operation. This grows the number of ops by 1.5
140/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000141///
Jay Foade98f29d2011-04-01 08:00:58 +0000142void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000143 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000144 unsigned NumOps = e + e / 2;
145 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
146
147 Use *OldOps = op_begin();
148 BasicBlock **OldBlocks = block_begin();
Jay Foade03c05c2011-06-20 14:38:01 +0000149
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000150 ReservedSpace = NumOps;
Jay Foad61ea0e42011-06-23 09:09:15 +0000151 OperandList = allocHungoffUses(ReservedSpace);
152
153 std::copy(OldOps, OldOps + e, op_begin());
154 std::copy(OldBlocks, OldBlocks + e, block_begin());
155
Jay Foadbbb91f22011-01-16 15:30:52 +0000156 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000157}
158
Nate Begemanb3923212005-08-04 23:24:19 +0000159/// hasConstantValue - If the specified PHI node always merges together the same
160/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000161Value *PHINode::hasConstantValue() const {
162 // Exploit the fact that phi nodes always have at least one entry.
163 Value *ConstantValue = getIncomingValue(0);
164 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000165 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
166 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000167 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000168 // The case where the first value is this PHI.
169 ConstantValue = getIncomingValue(i);
170 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000171 if (ConstantValue == this)
172 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000173 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000174}
175
Bill Wendlingfae14752011-08-12 20:24:12 +0000176//===----------------------------------------------------------------------===//
177// LandingPadInst Implementation
178//===----------------------------------------------------------------------===//
179
180LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
181 unsigned NumReservedValues, const Twine &NameStr,
182 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +0000183 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000184 init(PersonalityFn, 1 + NumReservedValues, NameStr);
185}
186
187LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
188 unsigned NumReservedValues, const Twine &NameStr,
189 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +0000190 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000191 init(PersonalityFn, 1 + NumReservedValues, NameStr);
192}
193
194LandingPadInst::LandingPadInst(const LandingPadInst &LP)
195 : Instruction(LP.getType(), Instruction::LandingPad,
196 allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()),
197 ReservedSpace(LP.getNumOperands()) {
198 Use *OL = OperandList, *InOL = LP.OperandList;
199 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
200 OL[I] = InOL[I];
201
202 setCleanup(LP.isCleanup());
203}
204
205LandingPadInst::~LandingPadInst() {
206 dropHungoffUses();
207}
208
209LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
210 unsigned NumReservedClauses,
211 const Twine &NameStr,
212 Instruction *InsertBefore) {
213 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
214 InsertBefore);
215}
216
217LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
218 unsigned NumReservedClauses,
219 const Twine &NameStr,
220 BasicBlock *InsertAtEnd) {
221 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
222 InsertAtEnd);
223}
224
225void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
226 const Twine &NameStr) {
227 ReservedSpace = NumReservedValues;
228 NumOperands = 1;
229 OperandList = allocHungoffUses(ReservedSpace);
230 OperandList[0] = PersFn;
231 setName(NameStr);
232 setCleanup(false);
233}
234
235/// growOperands - grow operands - This grows the operand list in response to a
236/// push_back style of operation. This grows the number of ops by 2 times.
237void LandingPadInst::growOperands(unsigned Size) {
238 unsigned e = getNumOperands();
239 if (ReservedSpace >= e + Size) return;
240 ReservedSpace = (e + Size / 2) * 2;
241
242 Use *NewOps = allocHungoffUses(ReservedSpace);
243 Use *OldOps = OperandList;
244 for (unsigned i = 0; i != e; ++i)
245 NewOps[i] = OldOps[i];
246
247 OperandList = NewOps;
248 Use::zap(OldOps, OldOps + e, true);
249}
250
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000251void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000252 unsigned OpNo = getNumOperands();
253 growOperands(1);
254 assert(OpNo < ReservedSpace && "Growing didn't work!");
255 ++NumOperands;
256 OperandList[OpNo] = Val;
257}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000258
259//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260// CallInst Implementation
261//===----------------------------------------------------------------------===//
262
Gordon Henriksen14a55692007-12-10 02:14:30 +0000263CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000264}
265
Jay Foad5bd375a2011-07-15 08:37:34 +0000266void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
267 assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000268 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269
Jay Foad5bd375a2011-07-15 08:37:34 +0000270#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000271 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
273
Jay Foad5bd375a2011-07-15 08:37:34 +0000274 assert((Args.size() == FTy->getNumParams() ||
275 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000276 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000277
278 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000279 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000280 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000281 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000282#endif
283
284 std::copy(Args.begin(), Args.end(), op_begin());
285 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286}
287
Jay Foad5bd375a2011-07-15 08:37:34 +0000288void CallInst::init(Value *Func, const Twine &NameStr) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000289 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000290 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000291
Jay Foad5bd375a2011-07-15 08:37:34 +0000292#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000293 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
295
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000296 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000297#endif
298
299 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Daniel Dunbar4975db62009-07-25 04:41:11 +0000302CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 Instruction *InsertBefore)
304 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000306 Instruction::Call,
307 OperandTraits<CallInst>::op_end(this) - 1,
308 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000309 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000310}
311
Daniel Dunbar4975db62009-07-25 04:41:11 +0000312CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313 BasicBlock *InsertAtEnd)
314 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
315 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000316 Instruction::Call,
317 OperandTraits<CallInst>::op_end(this) - 1,
318 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000319 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320}
321
Misha Brukmanb1c93172005-04-21 23:48:37 +0000322CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000323 : Instruction(CI.getType(), Instruction::Call,
324 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000325 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000326 setAttributes(CI.getAttributes());
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000327 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000328 setCallingConv(CI.getCallingConv());
329
Jay Foad5bd375a2011-07-15 08:37:34 +0000330 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000331 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332}
333
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000334void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000335 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000336 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000337 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000338}
339
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000340void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000341 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000342 AttrBuilder B(attr);
343 LLVMContext &Context = getContext();
344 PAL = PAL.removeAttributes(Context, i,
345 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000346 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000347}
348
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000349void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
350 AttributeSet PAL = getAttributes();
351 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
352 setAttributes(PAL);
353}
354
Michael Gottesman41748d72013-06-27 00:25:01 +0000355bool CallInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000356 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000357 return true;
358 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000359 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000360 return false;
361}
362
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000363bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000364 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000365 return true;
366 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000367 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000368 return false;
369}
370
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000371/// IsConstantOne - Return true only if val is constant int 1
372static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000373 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000374 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
375 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000376}
377
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000378static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000379 BasicBlock *InsertAtEnd, Type *IntPtrTy,
380 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000381 Value *ArraySize, Function *MallocF,
382 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000383 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000384 "createMalloc needs either InsertBefore or InsertAtEnd");
385
386 // malloc(type) becomes:
387 // bitcast (i8* malloc(typeSize)) to type*
388 // malloc(type, arraySize) becomes:
389 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000390 if (!ArraySize)
391 ArraySize = ConstantInt::get(IntPtrTy, 1);
392 else if (ArraySize->getType() != IntPtrTy) {
393 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000394 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
395 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000396 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000397 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
398 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000399 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000400
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000401 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000402 if (IsConstantOne(AllocSize)) {
403 AllocSize = ArraySize; // Operand * 1 = Operand
404 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
405 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
406 false /*ZExt*/);
407 // Malloc arg is constant product of type size and array size
408 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
409 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000410 // Multiply type size by the array size...
411 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000412 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
413 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000414 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000415 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
416 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000417 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000418 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000419
Victor Hernandez788eaab2009-09-18 19:20:02 +0000420 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000421 // Create the call to Malloc.
422 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
423 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000424 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000425 Value *MallocFunc = MallocF;
426 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000427 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000428 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000429 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000430 CallInst *MCall = nullptr;
431 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000432 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000433 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000434 Result = MCall;
435 if (Result->getType() != AllocPtrType)
436 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000437 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000438 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000439 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000440 Result = MCall;
441 if (Result->getType() != AllocPtrType) {
442 InsertAtEnd->getInstList().push_back(MCall);
443 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000444 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000445 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000446 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000447 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000448 if (Function *F = dyn_cast<Function>(MallocFunc)) {
449 MCall->setCallingConv(F->getCallingConv());
450 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
451 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000452 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000453
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000454 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000455}
456
457/// CreateMalloc - Generate the IR for a call to malloc:
458/// 1. Compute the malloc call's argument as the specified type's size,
459/// possibly multiplied by the array size if the array size is not
460/// constant 1.
461/// 2. Call malloc with that argument.
462/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000463Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000464 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000465 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000466 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000467 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000468 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000469 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000470}
471
472/// CreateMalloc - Generate the IR for a call to malloc:
473/// 1. Compute the malloc call's argument as the specified type's size,
474/// possibly multiplied by the array size if the array size is not
475/// constant 1.
476/// 2. Call malloc with that argument.
477/// 3. Bitcast the result of the malloc call to the specified type.
478/// Note: This function does not add the bitcast to the basic block, that is the
479/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000480Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000481 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000482 Value *AllocSize, Value *ArraySize,
483 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000484 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000485 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000486}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000487
Victor Hernandeze2971492009-10-24 04:23:03 +0000488static Instruction* createFree(Value* Source, Instruction *InsertBefore,
489 BasicBlock *InsertAtEnd) {
490 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
491 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000492 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000493 "Can not free something of nonpointer type!");
494
495 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
496 Module* M = BB->getParent()->getParent();
497
Chris Lattner229907c2011-07-18 04:54:35 +0000498 Type *VoidTy = Type::getVoidTy(M->getContext());
499 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000500 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000501 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000502 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000503 Value *PtrCast = Source;
504 if (InsertBefore) {
505 if (Source->getType() != IntPtrTy)
506 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
507 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
508 } else {
509 if (Source->getType() != IntPtrTy)
510 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
511 Result = CallInst::Create(FreeFunc, PtrCast, "");
512 }
513 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000514 if (Function *F = dyn_cast<Function>(FreeFunc))
515 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000516
517 return Result;
518}
519
520/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000521Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000522 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000523}
524
525/// CreateFree - Generate the IR for a call to the builtin free function.
526/// Note: This function does not add the call to the basic block, that is the
527/// responsibility of the caller.
528Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000529 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000530 assert(FreeCall && "CreateFree did not create a CallInst");
531 return FreeCall;
532}
533
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000534//===----------------------------------------------------------------------===//
535// InvokeInst Implementation
536//===----------------------------------------------------------------------===//
537
538void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Jay Foad5bd375a2011-07-15 08:37:34 +0000539 ArrayRef<Value *> Args, const Twine &NameStr) {
540 assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000541 Op<-3>() = Fn;
542 Op<-2>() = IfNormal;
543 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000544
545#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +0000546 FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000547 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000548
Jay Foad5bd375a2011-07-15 08:37:34 +0000549 assert(((Args.size() == FTy->getNumParams()) ||
550 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000551 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000552
Jay Foad5bd375a2011-07-15 08:37:34 +0000553 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000554 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000555 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000556 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000557#endif
558
559 std::copy(Args.begin(), Args.end(), op_begin());
560 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000561}
562
Misha Brukmanb1c93172005-04-21 23:48:37 +0000563InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000564 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000565 OperandTraits<InvokeInst>::op_end(this)
566 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000567 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000568 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000569 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000570 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000571 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000572}
573
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
575 return getSuccessor(idx);
576}
577unsigned InvokeInst::getNumSuccessorsV() const {
578 return getNumSuccessors();
579}
580void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
581 return setSuccessor(idx, B);
582}
583
Michael Gottesman41748d72013-06-27 00:25:01 +0000584bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000585 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000586 return true;
587 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000588 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000589 return false;
590}
591
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000592bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000593 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000594 return true;
595 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000596 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000597 return false;
598}
599
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000600void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000601 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000602 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000603 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000604}
605
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000606void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000607 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000608 AttrBuilder B(attr);
609 PAL = PAL.removeAttributes(getContext(), i,
610 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000611 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000612}
613
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000614void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
615 AttributeSet PAL = getAttributes();
616 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
617 setAttributes(PAL);
618}
619
Bill Wendlingfae14752011-08-12 20:24:12 +0000620LandingPadInst *InvokeInst::getLandingPadInst() const {
621 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
622}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000623
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000624//===----------------------------------------------------------------------===//
625// ReturnInst Implementation
626//===----------------------------------------------------------------------===//
627
Chris Lattner2195fc42007-02-24 00:55:48 +0000628ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000629 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000630 OperandTraits<ReturnInst>::op_end(this) -
631 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000632 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000633 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000634 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000635 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000636}
637
Owen Anderson55f1c092009-08-13 21:58:54 +0000638ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
639 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000640 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
641 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000642 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000643 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000644}
Owen Anderson55f1c092009-08-13 21:58:54 +0000645ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
646 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000647 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
648 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000649 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000650 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000651}
Owen Anderson55f1c092009-08-13 21:58:54 +0000652ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
653 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000654 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000655}
656
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657unsigned ReturnInst::getNumSuccessorsV() const {
658 return getNumSuccessors();
659}
660
Devang Patelae682fb2008-02-26 17:56:20 +0000661/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
662/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000663void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000664 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000665}
666
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000667BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000668 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000669}
670
Devang Patel59643e52008-02-23 00:35:18 +0000671ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000672}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000673
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000674//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000675// ResumeInst Implementation
676//===----------------------------------------------------------------------===//
677
678ResumeInst::ResumeInst(const ResumeInst &RI)
679 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
680 OperandTraits<ResumeInst>::op_begin(this), 1) {
681 Op<0>() = RI.Op<0>();
682}
683
684ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
685 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
686 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
687 Op<0>() = Exn;
688}
689
690ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
691 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
692 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
693 Op<0>() = Exn;
694}
695
696unsigned ResumeInst::getNumSuccessorsV() const {
697 return getNumSuccessors();
698}
699
700void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
701 llvm_unreachable("ResumeInst has no successors!");
702}
703
704BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
705 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000706}
707
708//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000709// UnreachableInst Implementation
710//===----------------------------------------------------------------------===//
711
Owen Anderson55f1c092009-08-13 21:58:54 +0000712UnreachableInst::UnreachableInst(LLVMContext &Context,
713 Instruction *InsertBefore)
714 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000715 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000716}
Owen Anderson55f1c092009-08-13 21:58:54 +0000717UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
718 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000719 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000720}
721
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000722unsigned UnreachableInst::getNumSuccessorsV() const {
723 return getNumSuccessors();
724}
725
726void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000727 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000728}
729
730BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000731 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000732}
733
734//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000735// BranchInst Implementation
736//===----------------------------------------------------------------------===//
737
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000738void BranchInst::AssertOK() {
739 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000740 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000741 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000742}
743
Chris Lattner2195fc42007-02-24 00:55:48 +0000744BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000745 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000746 OperandTraits<BranchInst>::op_end(this) - 1,
747 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000748 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000749 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000750}
751BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
752 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000753 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000754 OperandTraits<BranchInst>::op_end(this) - 3,
755 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000756 Op<-1>() = IfTrue;
757 Op<-2>() = IfFalse;
758 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000759#ifndef NDEBUG
760 AssertOK();
761#endif
762}
763
764BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000765 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000766 OperandTraits<BranchInst>::op_end(this) - 1,
767 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000768 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000769 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000770}
771
772BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
773 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000774 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000775 OperandTraits<BranchInst>::op_end(this) - 3,
776 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000777 Op<-1>() = IfTrue;
778 Op<-2>() = IfFalse;
779 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000780#ifndef NDEBUG
781 AssertOK();
782#endif
783}
784
785
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000786BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000787 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000788 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
789 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000790 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791 if (BI.getNumOperands() != 1) {
792 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000793 Op<-3>() = BI.Op<-3>();
794 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000795 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000796 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797}
798
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000799void BranchInst::swapSuccessors() {
800 assert(isConditional() &&
801 "Cannot swap successors of an unconditional branch");
802 Op<-1>().swap(Op<-2>());
803
804 // Update profile metadata if present and it matches our structural
805 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000806 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000807 if (!ProfileData || ProfileData->getNumOperands() != 3)
808 return;
809
810 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000811 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
812 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000813 setMetadata(LLVMContext::MD_prof,
814 MDNode::get(ProfileData->getContext(), Ops));
815}
816
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000817BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
818 return getSuccessor(idx);
819}
820unsigned BranchInst::getNumSuccessorsV() const {
821 return getNumSuccessors();
822}
823void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
824 setSuccessor(idx, B);
825}
826
827
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000829// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000830//===----------------------------------------------------------------------===//
831
Owen Andersonb6b25302009-07-14 23:09:55 +0000832static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000833 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000834 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000835 else {
836 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000837 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000838 assert(Amt->getType()->isIntegerTy() &&
839 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000840 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000841 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000842}
843
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000844AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
845 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000846
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000847AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
848 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000849
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000850AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000851 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000852 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000853
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000854AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000855 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000856 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000857
Chris Lattner229907c2011-07-18 04:54:35 +0000858AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000859 const Twine &Name, Instruction *InsertBefore)
860 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000861 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000862 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000863 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000864 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000865}
866
Chris Lattner229907c2011-07-18 04:54:35 +0000867AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000868 const Twine &Name, BasicBlock *InsertAtEnd)
869 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000870 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000871 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000872 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000873 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000874}
875
Gordon Henriksen14a55692007-12-10 02:14:30 +0000876// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000877AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000878}
879
Victor Hernandez8acf2952009-10-23 21:09:37 +0000880void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000881 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000882 assert(Align <= MaximumAlignment &&
883 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +0000884 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
885 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +0000886 assert(getAlignment() == Align && "Alignment representation error!");
887}
888
Victor Hernandez8acf2952009-10-23 21:09:37 +0000889bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000890 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000891 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000892 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000893}
894
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000895Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000896 return getType()->getElementType();
897}
898
Chris Lattner8b291e62008-11-26 02:54:17 +0000899/// isStaticAlloca - Return true if this alloca is in the entry block of the
900/// function and is a constant size. If so, the code generator will fold it
901/// into the prolog/epilog code, so it is basically free.
902bool AllocaInst::isStaticAlloca() const {
903 // Must be constant size.
904 if (!isa<ConstantInt>(getArraySize())) return false;
905
906 // Must be in the entry block.
907 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +0000908 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +0000909}
910
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000911//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000912// LoadInst Implementation
913//===----------------------------------------------------------------------===//
914
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000916 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000917 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000918 assert(!(isAtomic() && getAlignment() == 0) &&
919 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000920}
921
Daniel Dunbar4975db62009-07-25 04:41:11 +0000922LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000923 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000924
Daniel Dunbar4975db62009-07-25 04:41:11 +0000925LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000926 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000927
Daniel Dunbar4975db62009-07-25 04:41:11 +0000928LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929 Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000930 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +0000931
932LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
933 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000934 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +0000935
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000936LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000937 unsigned Align, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000938 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
939 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000940
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000941LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000942 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000943 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +0000944}
945
Eli Friedman59b66882011-08-09 23:02:53 +0000946LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
947 unsigned Align, AtomicOrdering Order,
948 SynchronizationScope SynchScope,
949 Instruction *InsertBef)
950 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
951 Load, Ptr, InsertBef) {
952 setVolatile(isVolatile);
953 setAlignment(Align);
954 setAtomic(Order, SynchScope);
955 AssertOK();
956 setName(Name);
957}
958
959LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
960 unsigned Align, AtomicOrdering Order,
961 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000962 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000963 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000964 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000965 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +0000966 setAlignment(Align);
967 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +0000968 AssertOK();
969 setName(Name);
970}
971
Daniel Dunbar27096822009-08-11 18:11:15 +0000972LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
973 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
974 Load, Ptr, InsertBef) {
975 setVolatile(false);
976 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000977 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000978 AssertOK();
979 if (Name && Name[0]) setName(Name);
980}
981
982LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
983 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
984 Load, Ptr, InsertAE) {
985 setVolatile(false);
986 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000987 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000988 AssertOK();
989 if (Name && Name[0]) setName(Name);
990}
991
992LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
993 Instruction *InsertBef)
994: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
995 Load, Ptr, InsertBef) {
996 setVolatile(isVolatile);
997 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000998 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000999 AssertOK();
1000 if (Name && Name[0]) setName(Name);
1001}
1002
1003LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1004 BasicBlock *InsertAE)
1005 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1006 Load, Ptr, InsertAE) {
1007 setVolatile(isVolatile);
1008 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001009 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001010 AssertOK();
1011 if (Name && Name[0]) setName(Name);
1012}
1013
Christopher Lamb84485702007-04-22 19:24:39 +00001014void LoadInst::setAlignment(unsigned Align) {
1015 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001016 assert(Align <= MaximumAlignment &&
1017 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001018 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001019 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001020 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001021}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001022
1023//===----------------------------------------------------------------------===//
1024// StoreInst Implementation
1025//===----------------------------------------------------------------------===//
1026
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001027void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001028 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001029 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001030 "Ptr must have pointer type!");
1031 assert(getOperand(0)->getType() ==
1032 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001033 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001034 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001035 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001036}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001037
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001038StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001039 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001040
1041StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001042 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001043
Misha Brukmanb1c93172005-04-21 23:48:37 +00001044StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001045 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001046 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001047
1048StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001049 BasicBlock *InsertAtEnd)
1050 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1051
1052StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1053 Instruction *InsertBefore)
1054 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1055 InsertBefore) {}
1056
1057StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1058 BasicBlock *InsertAtEnd)
1059 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1060 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001061
Misha Brukmanb1c93172005-04-21 23:48:37 +00001062StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001063 unsigned Align, AtomicOrdering Order,
1064 SynchronizationScope SynchScope,
1065 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001066 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001067 OperandTraits<StoreInst>::op_begin(this),
1068 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001069 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001070 Op<0>() = val;
1071 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001072 setVolatile(isVolatile);
1073 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001074 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001075 AssertOK();
1076}
1077
1078StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001079 unsigned Align, AtomicOrdering Order,
1080 SynchronizationScope SynchScope,
1081 BasicBlock *InsertAtEnd)
1082 : Instruction(Type::getVoidTy(val->getContext()), Store,
1083 OperandTraits<StoreInst>::op_begin(this),
1084 OperandTraits<StoreInst>::operands(this),
1085 InsertAtEnd) {
1086 Op<0>() = val;
1087 Op<1>() = addr;
1088 setVolatile(isVolatile);
1089 setAlignment(Align);
1090 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001091 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001092}
1093
Christopher Lamb84485702007-04-22 19:24:39 +00001094void StoreInst::setAlignment(unsigned Align) {
1095 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001096 assert(Align <= MaximumAlignment &&
1097 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001098 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001099 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001100 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001101}
1102
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001103//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001104// AtomicCmpXchgInst Implementation
1105//===----------------------------------------------------------------------===//
1106
1107void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001108 AtomicOrdering SuccessOrdering,
1109 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001110 SynchronizationScope SynchScope) {
1111 Op<0>() = Ptr;
1112 Op<1>() = Cmp;
1113 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001114 setSuccessOrdering(SuccessOrdering);
1115 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001116 setSynchScope(SynchScope);
1117
1118 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1119 "All operands must be non-null!");
1120 assert(getOperand(0)->getType()->isPointerTy() &&
1121 "Ptr must have pointer type!");
1122 assert(getOperand(1)->getType() ==
1123 cast<PointerType>(getOperand(0)->getType())->getElementType()
1124 && "Ptr must be a pointer to Cmp type!");
1125 assert(getOperand(2)->getType() ==
1126 cast<PointerType>(getOperand(0)->getType())->getElementType()
1127 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001128 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001129 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001130 assert(FailureOrdering != NotAtomic &&
1131 "AtomicCmpXchg instructions must be atomic!");
1132 assert(SuccessOrdering >= FailureOrdering &&
1133 "AtomicCmpXchg success ordering must be at least as strong as fail");
1134 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1135 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001136}
1137
1138AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001139 AtomicOrdering SuccessOrdering,
1140 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001141 SynchronizationScope SynchScope,
1142 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001143 : Instruction(
1144 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1145 nullptr),
1146 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1147 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001148 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001149}
1150
1151AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001152 AtomicOrdering SuccessOrdering,
1153 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001154 SynchronizationScope SynchScope,
1155 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001156 : Instruction(
1157 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1158 nullptr),
1159 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1160 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001161 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001162}
Tim Northover420a2162014-06-13 14:24:07 +00001163
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001164//===----------------------------------------------------------------------===//
1165// AtomicRMWInst Implementation
1166//===----------------------------------------------------------------------===//
1167
1168void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1169 AtomicOrdering Ordering,
1170 SynchronizationScope SynchScope) {
1171 Op<0>() = Ptr;
1172 Op<1>() = Val;
1173 setOperation(Operation);
1174 setOrdering(Ordering);
1175 setSynchScope(SynchScope);
1176
1177 assert(getOperand(0) && getOperand(1) &&
1178 "All operands must be non-null!");
1179 assert(getOperand(0)->getType()->isPointerTy() &&
1180 "Ptr must have pointer type!");
1181 assert(getOperand(1)->getType() ==
1182 cast<PointerType>(getOperand(0)->getType())->getElementType()
1183 && "Ptr must be a pointer to Val type!");
1184 assert(Ordering != NotAtomic &&
1185 "AtomicRMW instructions must be atomic!");
1186}
1187
1188AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1189 AtomicOrdering Ordering,
1190 SynchronizationScope SynchScope,
1191 Instruction *InsertBefore)
1192 : Instruction(Val->getType(), AtomicRMW,
1193 OperandTraits<AtomicRMWInst>::op_begin(this),
1194 OperandTraits<AtomicRMWInst>::operands(this),
1195 InsertBefore) {
1196 Init(Operation, Ptr, Val, Ordering, SynchScope);
1197}
1198
1199AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1200 AtomicOrdering Ordering,
1201 SynchronizationScope SynchScope,
1202 BasicBlock *InsertAtEnd)
1203 : Instruction(Val->getType(), AtomicRMW,
1204 OperandTraits<AtomicRMWInst>::op_begin(this),
1205 OperandTraits<AtomicRMWInst>::operands(this),
1206 InsertAtEnd) {
1207 Init(Operation, Ptr, Val, Ordering, SynchScope);
1208}
1209
1210//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001211// FenceInst Implementation
1212//===----------------------------------------------------------------------===//
1213
1214FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1215 SynchronizationScope SynchScope,
1216 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001217 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001218 setOrdering(Ordering);
1219 setSynchScope(SynchScope);
1220}
1221
1222FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1223 SynchronizationScope SynchScope,
1224 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001225 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001226 setOrdering(Ordering);
1227 setSynchScope(SynchScope);
1228}
1229
1230//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001231// GetElementPtrInst Implementation
1232//===----------------------------------------------------------------------===//
1233
Jay Foadd1b78492011-07-25 09:48:08 +00001234void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001235 const Twine &Name) {
Jay Foadd1b78492011-07-25 09:48:08 +00001236 assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1237 OperandList[0] = Ptr;
1238 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001239 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001240}
1241
Gabor Greiff6caff662008-05-10 08:32:32 +00001242GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001243 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001244 OperandTraits<GetElementPtrInst>::op_end(this)
1245 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001246 GEPI.getNumOperands()) {
Jay Foadd1b78492011-07-25 09:48:08 +00001247 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001248 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001249}
1250
Chris Lattner6090a422009-03-09 04:46:40 +00001251/// getIndexedType - Returns the type of the element that would be accessed with
1252/// a gep instruction with the specified parameters.
1253///
1254/// The Idxs pointer should point to a continuous piece of memory containing the
1255/// indices, either as Value* or uint64_t.
1256///
1257/// A null type is returned if the indices are invalid for the specified
1258/// pointer type.
1259///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001260template <typename IndexTy>
Jay Foadd1b78492011-07-25 09:48:08 +00001261static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
Duncan Sandse6beec62012-11-13 12:59:33 +00001262 PointerType *PTy = dyn_cast<PointerType>(Ptr->getScalarType());
Craig Topperc6207612014-04-09 06:08:46 +00001263 if (!PTy) return nullptr; // Type isn't a pointer type!
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001264 Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001265
Chris Lattner6090a422009-03-09 04:46:40 +00001266 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001267 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001268 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001269
Chris Lattner6090a422009-03-09 04:46:40 +00001270 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001271 // it cannot be 'stepped over'.
1272 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001273 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001274
Dan Gohman1ecaf452008-05-31 00:58:22 +00001275 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001276 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001277 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001278 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001279 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001280 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001281 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001282 }
Craig Topperc6207612014-04-09 06:08:46 +00001283 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001284}
1285
Jay Foadd1b78492011-07-25 09:48:08 +00001286Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1287 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001288}
1289
Chris Lattner229907c2011-07-18 04:54:35 +00001290Type *GetElementPtrInst::getIndexedType(Type *Ptr,
Jay Foadd1b78492011-07-25 09:48:08 +00001291 ArrayRef<Constant *> IdxList) {
1292 return getIndexedTypeInternal(Ptr, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001293}
1294
Jay Foadd1b78492011-07-25 09:48:08 +00001295Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1296 return getIndexedTypeInternal(Ptr, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001297}
1298
Chris Lattner45f15572007-04-14 00:12:57 +00001299/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1300/// zeros. If so, the result pointer and the first operand have the same
1301/// value, just potentially different types.
1302bool GetElementPtrInst::hasAllZeroIndices() const {
1303 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1304 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1305 if (!CI->isZero()) return false;
1306 } else {
1307 return false;
1308 }
1309 }
1310 return true;
1311}
1312
Chris Lattner27058292007-04-27 20:35:56 +00001313/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1314/// constant integers. If so, the result pointer and the first operand have
1315/// a constant offset between them.
1316bool GetElementPtrInst::hasAllConstantIndices() const {
1317 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1318 if (!isa<ConstantInt>(getOperand(i)))
1319 return false;
1320 }
1321 return true;
1322}
1323
Dan Gohman1b849082009-09-07 23:54:19 +00001324void GetElementPtrInst::setIsInBounds(bool B) {
1325 cast<GEPOperator>(this)->setIsInBounds(B);
1326}
Chris Lattner45f15572007-04-14 00:12:57 +00001327
Nick Lewycky28a5f252009-09-27 21:33:04 +00001328bool GetElementPtrInst::isInBounds() const {
1329 return cast<GEPOperator>(this)->isInBounds();
1330}
1331
Chandler Carruth1e140532012-12-11 10:29:10 +00001332bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1333 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001334 // Delegate to the generic GEPOperator implementation.
1335 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001336}
1337
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001338//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001339// ExtractElementInst Implementation
1340//===----------------------------------------------------------------------===//
1341
1342ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001343 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001344 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001345 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001346 ExtractElement,
1347 OperandTraits<ExtractElementInst>::op_begin(this),
1348 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001349 assert(isValidOperands(Val, Index) &&
1350 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001351 Op<0>() = Val;
1352 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001353 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001354}
1355
1356ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001357 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001358 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001359 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001360 ExtractElement,
1361 OperandTraits<ExtractElementInst>::op_begin(this),
1362 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001363 assert(isValidOperands(Val, Index) &&
1364 "Invalid extractelement instruction operands!");
1365
Gabor Greif2d3024d2008-05-26 21:33:52 +00001366 Op<0>() = Val;
1367 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001368 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001369}
1370
Chris Lattner65511ff2006-10-05 06:24:58 +00001371
Chris Lattner54865b32006-04-08 04:05:48 +00001372bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001373 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001374 return false;
1375 return true;
1376}
1377
1378
Robert Bocchino23004482006-01-10 19:05:34 +00001379//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001380// InsertElementInst Implementation
1381//===----------------------------------------------------------------------===//
1382
Chris Lattner54865b32006-04-08 04:05:48 +00001383InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001384 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001385 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001386 : Instruction(Vec->getType(), InsertElement,
1387 OperandTraits<InsertElementInst>::op_begin(this),
1388 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001389 assert(isValidOperands(Vec, Elt, Index) &&
1390 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001391 Op<0>() = Vec;
1392 Op<1>() = Elt;
1393 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001394 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001395}
1396
Chris Lattner54865b32006-04-08 04:05:48 +00001397InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001398 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001399 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001400 : Instruction(Vec->getType(), InsertElement,
1401 OperandTraits<InsertElementInst>::op_begin(this),
1402 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001403 assert(isValidOperands(Vec, Elt, Index) &&
1404 "Invalid insertelement instruction operands!");
1405
Gabor Greif2d3024d2008-05-26 21:33:52 +00001406 Op<0>() = Vec;
1407 Op<1>() = Elt;
1408 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001409 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001410}
1411
Chris Lattner54865b32006-04-08 04:05:48 +00001412bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1413 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001414 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001415 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001416
Reid Spencerd84d35b2007-02-15 02:26:10 +00001417 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001418 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001419
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001420 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001421 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001422 return true;
1423}
1424
1425
Robert Bocchinoca27f032006-01-17 20:07:22 +00001426//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001427// ShuffleVectorInst Implementation
1428//===----------------------------------------------------------------------===//
1429
1430ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001431 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001432 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001433: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001434 cast<VectorType>(Mask->getType())->getNumElements()),
1435 ShuffleVector,
1436 OperandTraits<ShuffleVectorInst>::op_begin(this),
1437 OperandTraits<ShuffleVectorInst>::operands(this),
1438 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001439 assert(isValidOperands(V1, V2, Mask) &&
1440 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001441 Op<0>() = V1;
1442 Op<1>() = V2;
1443 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001444 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001445}
1446
1447ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001448 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001449 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001450: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1451 cast<VectorType>(Mask->getType())->getNumElements()),
1452 ShuffleVector,
1453 OperandTraits<ShuffleVectorInst>::op_begin(this),
1454 OperandTraits<ShuffleVectorInst>::operands(this),
1455 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001456 assert(isValidOperands(V1, V2, Mask) &&
1457 "Invalid shuffle vector instruction operands!");
1458
Gabor Greif2d3024d2008-05-26 21:33:52 +00001459 Op<0>() = V1;
1460 Op<1>() = V2;
1461 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001462 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001463}
1464
Mon P Wang25f01062008-11-10 04:46:22 +00001465bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001466 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001467 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001468 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001469 return false;
1470
Chris Lattner1dcb6542012-01-25 23:49:49 +00001471 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001472 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001473 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001474 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001475
1476 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001477 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1478 return true;
1479
Nate Begeman60a31c32010-08-13 00:16:46 +00001480 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001481 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001482 for (Value *Op : MV->operands()) {
1483 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001484 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001485 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001486 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001487 return false;
1488 }
1489 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001490 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001491 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001492
1493 if (const ConstantDataSequential *CDS =
1494 dyn_cast<ConstantDataSequential>(Mask)) {
1495 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1496 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1497 if (CDS->getElementAsInteger(i) >= V1Size*2)
1498 return false;
1499 return true;
1500 }
1501
1502 // The bitcode reader can create a place holder for a forward reference
1503 // used as the shuffle mask. When this occurs, the shuffle mask will
1504 // fall into this case and fail. To avoid this error, do this bit of
1505 // ugliness to allow such a mask pass.
1506 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1507 if (CE->getOpcode() == Instruction::UserOp1)
1508 return true;
1509
1510 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001511}
1512
Chris Lattnerf724e342008-03-02 05:28:33 +00001513/// getMaskValue - Return the index from the shuffle mask for the specified
1514/// output result. This is either -1 if the element is undef or a number less
1515/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001516int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1517 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1518 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001519 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001520 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001521 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001522 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001523 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001524}
1525
Chris Lattner1dcb6542012-01-25 23:49:49 +00001526/// getShuffleMask - Return the full mask for this instruction, where each
1527/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001528void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1529 SmallVectorImpl<int> &Result) {
1530 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001531
Chris Lattnercf129702012-01-26 02:51:13 +00001532 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001533 for (unsigned i = 0; i != NumElts; ++i)
1534 Result.push_back(CDS->getElementAsInteger(i));
1535 return;
1536 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001537 for (unsigned i = 0; i != NumElts; ++i) {
1538 Constant *C = Mask->getAggregateElement(i);
1539 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001540 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001541 }
1542}
1543
1544
Dan Gohman12fce772008-05-15 19:50:34 +00001545//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001546// InsertValueInst Class
1547//===----------------------------------------------------------------------===//
1548
Jay Foad57aa6362011-07-13 10:26:04 +00001549void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1550 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001551 assert(NumOperands == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001552
1553 // There's no fundamental reason why we require at least one index
1554 // (other than weirdness with &*IdxBegin being invalid; see
1555 // getelementptr's init routine for example). But there's no
1556 // present need to support it.
1557 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1558
1559 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001560 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001561 Op<0>() = Agg;
1562 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001563
Jay Foad57aa6362011-07-13 10:26:04 +00001564 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001565 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001566}
1567
1568InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001569 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001570 OperandTraits<InsertValueInst>::op_begin(this), 2),
1571 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001572 Op<0>() = IVI.getOperand(0);
1573 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001574 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001575}
1576
1577//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001578// ExtractValueInst Class
1579//===----------------------------------------------------------------------===//
1580
Jay Foad57aa6362011-07-13 10:26:04 +00001581void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001582 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001583
Jay Foad57aa6362011-07-13 10:26:04 +00001584 // There's no fundamental reason why we require at least one index.
1585 // But there's no present need to support it.
1586 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001587
Jay Foad57aa6362011-07-13 10:26:04 +00001588 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001589 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001590}
1591
1592ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001593 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001594 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001595 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001596}
1597
Dan Gohman12fce772008-05-15 19:50:34 +00001598// getIndexedType - Returns the type of the element that would be extracted
1599// with an extractvalue instruction with the specified parameters.
1600//
1601// A null type is returned if the indices are invalid for the specified
1602// pointer type.
1603//
Chris Lattner229907c2011-07-18 04:54:35 +00001604Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001605 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001606 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001607 // We can't use CompositeType::indexValid(Index) here.
1608 // indexValid() always returns true for arrays because getelementptr allows
1609 // out-of-bounds indices. Since we don't allow those for extractvalue and
1610 // insertvalue we need to check array indexing manually.
1611 // Since the only other types we can index into are struct types it's just
1612 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001613 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001614 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001615 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001616 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001617 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001618 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001619 } else {
1620 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001621 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001622 }
1623
1624 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001625 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001626 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001627}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001628
1629//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001630// BinaryOperator Class
1631//===----------------------------------------------------------------------===//
1632
Chris Lattner2195fc42007-02-24 00:55:48 +00001633BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001634 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001635 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001636 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001637 OperandTraits<BinaryOperator>::op_begin(this),
1638 OperandTraits<BinaryOperator>::operands(this),
1639 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001640 Op<0>() = S1;
1641 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001642 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001643 setName(Name);
1644}
1645
1646BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001647 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001648 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001649 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001650 OperandTraits<BinaryOperator>::op_begin(this),
1651 OperandTraits<BinaryOperator>::operands(this),
1652 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001653 Op<0>() = S1;
1654 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001655 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001656 setName(Name);
1657}
1658
1659
1660void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001661 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001662 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001663 assert(LHS->getType() == RHS->getType() &&
1664 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001665#ifndef NDEBUG
1666 switch (iType) {
1667 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001668 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001669 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001670 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001671 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001672 "Tried to create an integer operation on a non-integer type!");
1673 break;
1674 case FAdd: case FSub:
1675 case FMul:
1676 assert(getType() == LHS->getType() &&
1677 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001678 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001679 "Tried to create a floating-point operation on a "
1680 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001681 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001682 case UDiv:
1683 case SDiv:
1684 assert(getType() == LHS->getType() &&
1685 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001686 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001687 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001688 "Incorrect operand type (not integer) for S/UDIV");
1689 break;
1690 case FDiv:
1691 assert(getType() == LHS->getType() &&
1692 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001693 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001694 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001695 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001696 case URem:
1697 case SRem:
1698 assert(getType() == LHS->getType() &&
1699 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001700 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001701 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001702 "Incorrect operand type (not integer) for S/UREM");
1703 break;
1704 case FRem:
1705 assert(getType() == LHS->getType() &&
1706 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001707 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001708 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001709 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001710 case Shl:
1711 case LShr:
1712 case AShr:
1713 assert(getType() == LHS->getType() &&
1714 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001715 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001716 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001717 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001718 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001719 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001720 case And: case Or:
1721 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001722 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001723 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001724 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001725 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001726 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001727 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001728 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001729 default:
1730 break;
1731 }
1732#endif
1733}
1734
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001735BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001736 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001737 Instruction *InsertBefore) {
1738 assert(S1->getType() == S2->getType() &&
1739 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001740 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001741}
1742
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001743BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001744 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001745 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001746 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001747 InsertAtEnd->getInstList().push_back(Res);
1748 return Res;
1749}
1750
Dan Gohman5476cfd2009-08-12 16:23:25 +00001751BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001752 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001753 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001754 return new BinaryOperator(Instruction::Sub,
1755 zero, Op,
1756 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001757}
1758
Dan Gohman5476cfd2009-08-12 16:23:25 +00001759BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001760 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001761 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001762 return new BinaryOperator(Instruction::Sub,
1763 zero, Op,
1764 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001765}
1766
Dan Gohman4ab44202009-12-18 02:58:50 +00001767BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1768 Instruction *InsertBefore) {
1769 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1770 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1771}
1772
1773BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1774 BasicBlock *InsertAtEnd) {
1775 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1776 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1777}
1778
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001779BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1780 Instruction *InsertBefore) {
1781 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1782 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1783}
1784
1785BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1786 BasicBlock *InsertAtEnd) {
1787 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1788 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1789}
1790
Dan Gohman5476cfd2009-08-12 16:23:25 +00001791BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001792 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001793 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001794 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001795 Op->getType(), Name, InsertBefore);
1796}
1797
Dan Gohman5476cfd2009-08-12 16:23:25 +00001798BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001799 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001800 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001801 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001802 Op->getType(), Name, InsertAtEnd);
1803}
1804
Dan Gohman5476cfd2009-08-12 16:23:25 +00001805BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001806 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001807 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001808 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001809 Op->getType(), Name, InsertBefore);
1810}
1811
Dan Gohman5476cfd2009-08-12 16:23:25 +00001812BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001813 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001814 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001815 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001816 Op->getType(), Name, InsertAtEnd);
1817}
1818
1819
1820// isConstantAllOnes - Helper function for several functions below
1821static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001822 if (const Constant *C = dyn_cast<Constant>(V))
1823 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001824 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001825}
1826
Owen Andersonbb2501b2009-07-13 22:18:28 +00001827bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001828 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1829 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001830 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1831 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001832 return false;
1833}
1834
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001835bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001836 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1837 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001838 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
1839 if (!IgnoreZeroSign)
1840 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
1841 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
1842 }
Dan Gohmana5b96452009-06-04 22:49:04 +00001843 return false;
1844}
1845
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001846bool BinaryOperator::isNot(const Value *V) {
1847 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1848 return (Bop->getOpcode() == Instruction::Xor &&
1849 (isConstantAllOnes(Bop->getOperand(1)) ||
1850 isConstantAllOnes(Bop->getOperand(0))));
1851 return false;
1852}
1853
Chris Lattner2c7d1772005-04-24 07:28:37 +00001854Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001855 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001856}
1857
Chris Lattner2c7d1772005-04-24 07:28:37 +00001858const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1859 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001860}
1861
Dan Gohmana5b96452009-06-04 22:49:04 +00001862Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001863 return cast<BinaryOperator>(BinOp)->getOperand(1);
1864}
1865
1866const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1867 return getFNegArgument(const_cast<Value*>(BinOp));
1868}
1869
Chris Lattner2c7d1772005-04-24 07:28:37 +00001870Value *BinaryOperator::getNotArgument(Value *BinOp) {
1871 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1872 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1873 Value *Op0 = BO->getOperand(0);
1874 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001875 if (isConstantAllOnes(Op0)) return Op1;
1876
1877 assert(isConstantAllOnes(Op1));
1878 return Op0;
1879}
1880
Chris Lattner2c7d1772005-04-24 07:28:37 +00001881const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1882 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001883}
1884
1885
1886// swapOperands - Exchange the two operands to this instruction. This
1887// instruction is safe to use on any binary instruction and does not
1888// modify the semantics of the instruction. If the instruction is
1889// order dependent (SetLT f.e.) the opcode is changed.
1890//
1891bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001892 if (!isCommutative())
1893 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001894 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001895 return false;
1896}
1897
Dan Gohman1b849082009-09-07 23:54:19 +00001898void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1899 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1900}
1901
1902void BinaryOperator::setHasNoSignedWrap(bool b) {
1903 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1904}
1905
1906void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001907 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001908}
1909
Nick Lewycky28a5f252009-09-27 21:33:04 +00001910bool BinaryOperator::hasNoUnsignedWrap() const {
1911 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1912}
1913
1914bool BinaryOperator::hasNoSignedWrap() const {
1915 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1916}
1917
1918bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001919 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001920}
1921
Sanjay Patela982d992014-09-03 01:06:50 +00001922void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00001923 // Copy the wrapping flags.
1924 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
1925 setHasNoSignedWrap(OB->hasNoSignedWrap());
1926 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
1927 }
1928
1929 // Copy the exact flag.
1930 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
1931 setIsExact(PE->isExact());
1932
1933 // Copy the fast-math flags.
1934 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00001935 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00001936}
1937
Sanjay Patela982d992014-09-03 01:06:50 +00001938void BinaryOperator::andIRFlags(const Value *V) {
1939 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
1940 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
1941 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
1942 }
1943
1944 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
1945 setIsExact(isExact() & PE->isExact());
1946
1947 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
1948 FastMathFlags FM = getFastMathFlags();
1949 FM &= FP->getFastMathFlags();
1950 copyFastMathFlags(FM);
1951 }
1952}
1953
1954
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001955//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00001956// FPMathOperator Class
1957//===----------------------------------------------------------------------===//
1958
1959/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
1960/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00001961/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00001962float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001963 const MDNode *MD =
1964 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00001965 if (!MD)
1966 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001967 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00001968 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00001969}
1970
1971
1972//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001973// CastInst Class
1974//===----------------------------------------------------------------------===//
1975
David Blaikie3a15e142011-12-01 08:00:17 +00001976void CastInst::anchor() {}
1977
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001978// Just determine if this cast only deals with integral->integral conversion.
1979bool CastInst::isIntegerCast() const {
1980 switch (getOpcode()) {
1981 default: return false;
1982 case Instruction::ZExt:
1983 case Instruction::SExt:
1984 case Instruction::Trunc:
1985 return true;
1986 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001987 return getOperand(0)->getType()->isIntegerTy() &&
1988 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001989 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001990}
1991
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001992bool CastInst::isLosslessCast() const {
1993 // Only BitCast can be lossless, exit fast if we're not BitCast
1994 if (getOpcode() != Instruction::BitCast)
1995 return false;
1996
1997 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00001998 Type* SrcTy = getOperand(0)->getType();
1999 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002000 if (SrcTy == DstTy)
2001 return true;
2002
Reid Spencer8d9336d2006-12-31 05:26:44 +00002003 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002004 if (SrcTy->isPointerTy())
2005 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002006 return false; // Other types have no identity values
2007}
2008
2009/// This function determines if the CastInst does not require any bits to be
2010/// changed in order to effect the cast. Essentially, it identifies cases where
2011/// no code gen is necessary for the cast, hence the name no-op cast. For
2012/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002013/// # bitcast i32* %x to i8*
2014/// # bitcast <2 x i32> %x to <4 x i16>
2015/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002016/// @brief Determine if the described cast is a no-op.
2017bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002018 Type *SrcTy,
2019 Type *DestTy,
2020 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002021 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002022 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002023 case Instruction::Trunc:
2024 case Instruction::ZExt:
2025 case Instruction::SExt:
2026 case Instruction::FPTrunc:
2027 case Instruction::FPExt:
2028 case Instruction::UIToFP:
2029 case Instruction::SIToFP:
2030 case Instruction::FPToUI:
2031 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002032 case Instruction::AddrSpaceCast:
2033 // TODO: Target informations may give a more accurate answer here.
2034 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002035 case Instruction::BitCast:
2036 return true; // BitCast never modifies bits.
2037 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002038 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002039 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002040 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002041 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002042 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002043 }
2044}
2045
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002046/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002047bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002048 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2049}
2050
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002051bool CastInst::isNoopCast(const DataLayout *DL) const {
2052 if (!DL) {
2053 // Assume maximum pointer size.
2054 return isNoopCast(Type::getInt64Ty(getContext()));
2055 }
2056
Craig Topperc6207612014-04-09 06:08:46 +00002057 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002058 if (getOpcode() == Instruction::PtrToInt)
2059 PtrOpTy = getOperand(0)->getType();
2060 else if (getOpcode() == Instruction::IntToPtr)
2061 PtrOpTy = getType();
2062
2063 Type *IntPtrTy = PtrOpTy
2064 ? DL->getIntPtrType(PtrOpTy)
2065 : DL->getIntPtrType(getContext(), 0);
2066
2067 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2068}
2069
2070/// This function determines if a pair of casts can be eliminated and what
2071/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002072/// instructions like this:
2073/// * %F = firstOpcode SrcTy %x to MidTy
2074/// * %S = secondOpcode MidTy %F to DstTy
2075/// The function returns a resultOpcode so these two casts can be replaced with:
2076/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2077/// If no such cast is permited, the function returns 0.
2078unsigned CastInst::isEliminableCastPair(
2079 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002080 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2081 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002082 // Define the 144 possibilities for these two cast instructions. The values
2083 // in this matrix determine what to do in a given situation and select the
2084 // case in the switch below. The rows correspond to firstOp, the columns
2085 // correspond to secondOp. In looking at the table below, keep in mind
2086 // the following cast properties:
2087 //
2088 // Size Compare Source Destination
2089 // Operator Src ? Size Type Sign Type Sign
2090 // -------- ------------ ------------------- ---------------------
2091 // TRUNC > Integer Any Integral Any
2092 // ZEXT < Integral Unsigned Integer Any
2093 // SEXT < Integral Signed Integer Any
2094 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002095 // FPTOSI n/a FloatPt n/a Integral Signed
2096 // UITOFP n/a Integral Unsigned FloatPt n/a
2097 // SITOFP n/a Integral Signed FloatPt n/a
2098 // FPTRUNC > FloatPt n/a FloatPt n/a
2099 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002100 // PTRTOINT n/a Pointer n/a Integral Unsigned
2101 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002102 // BITCAST = FirstClass n/a FirstClass n/a
2103 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002104 //
2105 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002106 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2107 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002108 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002109 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002110 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002111 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002112 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2114 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002115 // T F F U S F F P I B A -+
2116 // R Z S P P I I T P 2 N T S |
2117 // U E E 2 2 2 2 R E I T C C +- secondOp
2118 // N X X U S F F N X N 2 V V |
2119 // C T T I I P P C T T P T T -+
2120 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
2121 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3, 0}, // ZExt |
2122 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2123 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2124 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2125 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2126 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
2127 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4, 0}, // FPTrunc |
2128 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2129 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2130 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2131 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2132 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002133 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002134
Chris Lattner25eea4d2010-07-12 01:19:22 +00002135 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002136 // merging. However, bitcast of A->B->A are allowed.
2137 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2138 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2139 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2140
2141 // Check if any of the bitcasts convert scalars<->vectors.
2142 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2143 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2144 // Unless we are bitcasing to the original type, disallow optimizations.
2145 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002146
2147 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2148 [secondOp-Instruction::CastOpsBegin];
2149 switch (ElimCase) {
2150 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002151 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002152 return 0;
2153 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002154 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002155 return firstOp;
2156 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002157 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002158 return secondOp;
2159 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002160 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002161 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002162 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002163 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002164 return firstOp;
2165 return 0;
2166 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002167 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002168 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002169 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002170 return firstOp;
2171 return 0;
2172 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002173 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002174 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002175 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176 return secondOp;
2177 return 0;
2178 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002179 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002180 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002181 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002182 return secondOp;
2183 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002184 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002185 // Cannot simplify if address spaces are different!
2186 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2187 return 0;
2188
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002189 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002190 // We can still fold this without knowing the actual sizes as long we
2191 // know that the intermediate pointer is the largest possible
2192 // pointer size.
2193 // FIXME: Is this always true?
2194 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002195 return Instruction::BitCast;
2196
2197 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002198 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002199 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002200 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201 if (MidSize >= PtrSize)
2202 return Instruction::BitCast;
2203 return 0;
2204 }
2205 case 8: {
2206 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2207 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2208 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002209 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2210 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211 if (SrcSize == DstSize)
2212 return Instruction::BitCast;
2213 else if (SrcSize < DstSize)
2214 return firstOp;
2215 return secondOp;
2216 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002217 case 9:
2218 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219 return Instruction::ZExt;
2220 case 10:
2221 // fpext followed by ftrunc is allowed if the bit size returned to is
2222 // the same as the original, in which case its just a bitcast
2223 if (SrcTy == DstTy)
2224 return Instruction::BitCast;
2225 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002226 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002227 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002228 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002229 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002230 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002231 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2232 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002233 if (SrcSize <= PtrSize && SrcSize == DstSize)
2234 return Instruction::BitCast;
2235 return 0;
2236 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002237 case 12: {
2238 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2239 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2240 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2241 return Instruction::AddrSpaceCast;
2242 return Instruction::BitCast;
2243 }
2244 case 13:
2245 // FIXME: this state can be merged with (1), but the following assert
2246 // is useful to check the correcteness of the sequence due to semantic
2247 // change of bitcast.
2248 assert(
2249 SrcTy->isPtrOrPtrVectorTy() &&
2250 MidTy->isPtrOrPtrVectorTy() &&
2251 DstTy->isPtrOrPtrVectorTy() &&
2252 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2253 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2254 "Illegal addrspacecast, bitcast sequence!");
2255 // Allowed, use first cast's opcode
2256 return firstOp;
2257 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002258 // bitcast, addrspacecast -> addrspacecast if the element type of
2259 // bitcast's source is the same as that of addrspacecast's destination.
2260 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2261 return Instruction::AddrSpaceCast;
2262 return 0;
2263
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002264 case 15:
2265 // FIXME: this state can be merged with (1), but the following assert
2266 // is useful to check the correcteness of the sequence due to semantic
2267 // change of bitcast.
2268 assert(
2269 SrcTy->isIntOrIntVectorTy() &&
2270 MidTy->isPtrOrPtrVectorTy() &&
2271 DstTy->isPtrOrPtrVectorTy() &&
2272 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2273 "Illegal inttoptr, bitcast sequence!");
2274 // Allowed, use first cast's opcode
2275 return firstOp;
2276 case 16:
2277 // FIXME: this state can be merged with (2), but the following assert
2278 // is useful to check the correcteness of the sequence due to semantic
2279 // change of bitcast.
2280 assert(
2281 SrcTy->isPtrOrPtrVectorTy() &&
2282 MidTy->isPtrOrPtrVectorTy() &&
2283 DstTy->isIntOrIntVectorTy() &&
2284 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2285 "Illegal bitcast, ptrtoint sequence!");
2286 // Allowed, use second cast's opcode
2287 return secondOp;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002288 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002289 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002290 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002291 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002292 default:
Craig Topperc514b542012-02-05 22:14:15 +00002293 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295}
2296
Chris Lattner229907c2011-07-18 04:54:35 +00002297CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002298 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002299 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002300 // Construct and return the appropriate CastInst subclass
2301 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002302 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2303 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2304 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2305 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2306 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2307 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2308 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2309 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2310 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2311 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2312 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2313 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2314 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2315 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317}
2318
Chris Lattner229907c2011-07-18 04:54:35 +00002319CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002320 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002321 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002322 // Construct and return the appropriate CastInst subclass
2323 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002324 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2325 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2326 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2327 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2328 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2329 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2330 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2331 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2332 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2333 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2334 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2335 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2336 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2337 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002338 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339}
2340
Chris Lattner229907c2011-07-18 04:54:35 +00002341CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002342 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002343 Instruction *InsertBefore) {
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, InsertBefore);
2346 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002347}
2348
Chris Lattner229907c2011-07-18 04:54:35 +00002349CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002350 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002351 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
2354 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002355}
2356
Chris Lattner229907c2011-07-18 04:54:35 +00002357CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002358 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002359 Instruction *InsertBefore) {
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, InsertBefore);
2362 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002363}
2364
Chris Lattner229907c2011-07-18 04:54:35 +00002365CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002366 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002367 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002368 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002369 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2370 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002371}
2372
Chris Lattner229907c2011-07-18 04:54:35 +00002373CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002374 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002375 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002376 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002377 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2378 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002379}
2380
Chris Lattner229907c2011-07-18 04:54:35 +00002381CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002382 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002383 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002384 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002385 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2386 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002387}
2388
Chris Lattner229907c2011-07-18 04:54:35 +00002389CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002390 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002391 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002392 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2393 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2394 "Invalid cast");
2395 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002396 assert((!Ty->isVectorTy() ||
2397 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002398 "Invalid cast");
2399
Matt Arsenault065ced92013-07-31 00:17:33 +00002400 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002401 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002402
Matt Arsenault740980e2014-07-14 17:24:35 +00002403 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002404}
2405
2406/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002407CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2408 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002409 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002410 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2411 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002412 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002413 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002414 assert((!Ty->isVectorTy() ||
2415 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002416 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002417
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002418 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002419 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002420
Matt Arsenault740980e2014-07-14 17:24:35 +00002421 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2422}
2423
2424CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2425 Value *S, Type *Ty,
2426 const Twine &Name,
2427 BasicBlock *InsertAtEnd) {
2428 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2429 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2430
2431 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2432 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2433
2434 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2435}
2436
2437CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2438 Value *S, Type *Ty,
2439 const Twine &Name,
2440 Instruction *InsertBefore) {
2441 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2442 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2443
2444 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002445 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2446
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002447 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002448}
2449
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002450CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2451 const Twine &Name,
2452 Instruction *InsertBefore) {
2453 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2454 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2455 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2456 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2457
2458 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2459}
2460
Matt Arsenault740980e2014-07-14 17:24:35 +00002461CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002462 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002463 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002464 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002465 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002466 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2467 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002468 Instruction::CastOps opcode =
2469 (SrcBits == DstBits ? Instruction::BitCast :
2470 (SrcBits > DstBits ? Instruction::Trunc :
2471 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002472 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002473}
2474
Chris Lattner229907c2011-07-18 04:54:35 +00002475CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002476 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002477 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002478 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002479 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002480 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2481 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002482 Instruction::CastOps opcode =
2483 (SrcBits == DstBits ? Instruction::BitCast :
2484 (SrcBits > DstBits ? Instruction::Trunc :
2485 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002486 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002487}
2488
Chris Lattner229907c2011-07-18 04:54:35 +00002489CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002490 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002491 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002492 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002493 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002494 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2495 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002496 Instruction::CastOps opcode =
2497 (SrcBits == DstBits ? Instruction::BitCast :
2498 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002499 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002500}
2501
Chris Lattner229907c2011-07-18 04:54:35 +00002502CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002503 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002504 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002505 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002506 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002507 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2508 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002509 Instruction::CastOps opcode =
2510 (SrcBits == DstBits ? Instruction::BitCast :
2511 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002512 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002513}
2514
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002515// Check whether it is valid to call getCastOpcode for these types.
2516// This routine must be kept in sync with getCastOpcode.
2517bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2518 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2519 return false;
2520
2521 if (SrcTy == DestTy)
2522 return true;
2523
2524 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2525 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2526 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2527 // An element by element cast. Valid if casting the elements is valid.
2528 SrcTy = SrcVecTy->getElementType();
2529 DestTy = DestVecTy->getElementType();
2530 }
2531
2532 // Get the bit sizes, we'll need these
2533 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2534 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2535
2536 // Run through the possibilities ...
2537 if (DestTy->isIntegerTy()) { // Casting to integral
2538 if (SrcTy->isIntegerTy()) { // Casting from integral
2539 return true;
2540 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2541 return true;
2542 } else if (SrcTy->isVectorTy()) { // Casting from vector
2543 return DestBits == SrcBits;
2544 } else { // Casting from something else
2545 return SrcTy->isPointerTy();
2546 }
2547 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2548 if (SrcTy->isIntegerTy()) { // Casting from integral
2549 return true;
2550 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
2551 return true;
2552 } else if (SrcTy->isVectorTy()) { // Casting from vector
2553 return DestBits == SrcBits;
2554 } else { // Casting from something else
2555 return false;
2556 }
2557 } else if (DestTy->isVectorTy()) { // Casting to vector
2558 return DestBits == SrcBits;
2559 } else if (DestTy->isPointerTy()) { // Casting to pointer
2560 if (SrcTy->isPointerTy()) { // Casting from pointer
2561 return true;
2562 } else if (SrcTy->isIntegerTy()) { // Casting from integral
2563 return true;
2564 } else { // Casting from something else
2565 return false;
2566 }
2567 } else if (DestTy->isX86_MMXTy()) {
2568 if (SrcTy->isVectorTy()) {
2569 return DestBits == SrcBits; // 64-bit vector to MMX
2570 } else {
2571 return false;
2572 }
2573 } else { // Casting to something else
2574 return false;
2575 }
2576}
2577
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002578bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2579 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2580 return false;
2581
2582 if (SrcTy == DestTy)
2583 return true;
2584
2585 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2586 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2587 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2588 // An element by element cast. Valid if casting the elements is valid.
2589 SrcTy = SrcVecTy->getElementType();
2590 DestTy = DestVecTy->getElementType();
2591 }
2592 }
2593 }
2594
2595 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2596 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2597 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2598 }
2599 }
2600
2601 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2602 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2603
2604 // Could still have vectors of pointers if the number of elements doesn't
2605 // match
2606 if (SrcBits == 0 || DestBits == 0)
2607 return false;
2608
2609 if (SrcBits != DestBits)
2610 return false;
2611
2612 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2613 return false;
2614
2615 return true;
2616}
2617
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002618bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
2619 const DataLayout *DL) {
2620 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2621 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
2622 return DL && IntTy->getBitWidth() == DL->getPointerTypeSizeInBits(PtrTy);
2623 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2624 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
2625 return DL && IntTy->getBitWidth() == DL->getPointerTypeSizeInBits(PtrTy);
2626
2627 return isBitCastable(SrcTy, DestTy);
2628}
2629
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002630// Provide a way to get a "cast" where the cast opcode is inferred from the
2631// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002632// logic in the castIsValid function below. This axiom should hold:
2633// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2634// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002636// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002638CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002639 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2640 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002641
Duncan Sands55e50902008-01-06 10:12:28 +00002642 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2643 "Only first class types are castable!");
2644
Duncan Sandsa8514532011-05-18 07:13:41 +00002645 if (SrcTy == DestTy)
2646 return BitCast;
2647
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002648 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002649 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2650 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002651 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2652 // An element by element cast. Find the appropriate opcode based on the
2653 // element types.
2654 SrcTy = SrcVecTy->getElementType();
2655 DestTy = DestVecTy->getElementType();
2656 }
2657
2658 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002659 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2660 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002661
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002662 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002663 if (DestTy->isIntegerTy()) { // Casting to integral
2664 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665 if (DestBits < SrcBits)
2666 return Trunc; // int -> smaller int
2667 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002668 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002669 return SExt; // signed -> SEXT
2670 else
2671 return ZExt; // unsigned -> ZEXT
2672 } else {
2673 return BitCast; // Same size, No-op cast
2674 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002675 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002676 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677 return FPToSI; // FP -> sint
2678 else
2679 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002680 } else if (SrcTy->isVectorTy()) {
2681 assert(DestBits == SrcBits &&
2682 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002683 return BitCast; // Same size, no-op cast
2684 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002685 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002686 "Casting from a value that is not first-class type");
2687 return PtrToInt; // ptr -> int
2688 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002689 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2690 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002691 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002692 return SIToFP; // sint -> FP
2693 else
2694 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002695 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002696 if (DestBits < SrcBits) {
2697 return FPTrunc; // FP -> smaller FP
2698 } else if (DestBits > SrcBits) {
2699 return FPExt; // FP -> larger FP
2700 } else {
2701 return BitCast; // same size, no-op cast
2702 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002703 } else if (SrcTy->isVectorTy()) {
2704 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002705 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002706 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002707 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002708 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002709 } else if (DestTy->isVectorTy()) {
2710 assert(DestBits == SrcBits &&
2711 "Illegal cast to vector (wrong type or size)");
2712 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002713 } else if (DestTy->isPointerTy()) {
2714 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002715 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2716 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002717 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002718 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002719 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002720 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002721 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002722 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002723 if (SrcTy->isVectorTy()) {
2724 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002725 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002726 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002727 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002728 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002729 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002730}
2731
2732//===----------------------------------------------------------------------===//
2733// CastInst SubClass Constructors
2734//===----------------------------------------------------------------------===//
2735
2736/// Check that the construction parameters for a CastInst are correct. This
2737/// could be broken out into the separate constructors but it is useful to have
2738/// it in one place and to eliminate the redundant code for getting the sizes
2739/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002740bool
Chris Lattner229907c2011-07-18 04:54:35 +00002741CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002742
2743 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002744 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002745
Chris Lattner37bc78a2010-01-26 21:51:43 +00002746 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2747 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002748 return false;
2749
2750 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002751 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2752 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002753
Duncan Sands7f646562011-05-18 09:21:57 +00002754 // If these are vector types, get the lengths of the vectors (using zero for
2755 // scalar types means that checking that vector lengths match also checks that
2756 // scalars are not being converted to vectors or vectors to scalars).
2757 unsigned SrcLength = SrcTy->isVectorTy() ?
2758 cast<VectorType>(SrcTy)->getNumElements() : 0;
2759 unsigned DstLength = DstTy->isVectorTy() ?
2760 cast<VectorType>(DstTy)->getNumElements() : 0;
2761
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002762 // Switch on the opcode provided
2763 switch (op) {
2764 default: return false; // This is an input error
2765 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002766 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2767 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002768 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002769 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2770 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002771 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002772 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2773 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002774 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002775 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2776 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002777 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002778 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2779 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002780 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002781 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002782 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2783 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002784 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002785 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002786 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2787 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002788 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002789 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002790 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002791 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2792 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2793 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002794 return SrcTy->getScalarType()->isPointerTy() &&
2795 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002796 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002797 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002798 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002799 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2800 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2801 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002802 return SrcTy->getScalarType()->isIntegerTy() &&
2803 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002804 case Instruction::BitCast: {
2805 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2806 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2807
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002808 // BitCast implies a no-op cast of type only. No bits change.
2809 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002810 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002811 return false;
2812
Alp Tokerf907b892013-12-05 05:44:44 +00002813 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002814 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002815 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002816 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2817
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002818 // If both are pointers then the address spaces must match.
2819 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
2820 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002821
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002822 // A vector of pointers must have the same number of elements.
2823 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2824 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2825 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2826
2827 return false;
2828 }
2829
2830 return true;
2831 }
2832 case Instruction::AddrSpaceCast: {
2833 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2834 if (!SrcPtrTy)
2835 return false;
2836
2837 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2838 if (!DstPtrTy)
2839 return false;
2840
2841 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
2842 return false;
2843
2844 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2845 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2846 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2847
2848 return false;
2849 }
2850
2851 return true;
2852 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002853 }
2854}
2855
2856TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002857 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002858) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002859 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002860}
2861
2862TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002863 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002864) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002865 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002866}
2867
2868ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002869 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002870) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002871 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002872}
2873
2874ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002875 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002876) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002877 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002878}
2879SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002880 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002881) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002882 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002883}
2884
Jeff Cohencc08c832006-12-02 02:22:01 +00002885SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002886 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002887) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002888 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002889}
2890
2891FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002892 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002893) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002894 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002895}
2896
2897FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002898 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002899) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002900 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002901}
2902
2903FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002904 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002905) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002906 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002907}
2908
2909FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002910 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002911) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002912 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002913}
2914
2915UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002916 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002917) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002918 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002919}
2920
2921UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002922 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002923) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002924 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002925}
2926
2927SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002928 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002929) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002930 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002931}
2932
2933SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002934 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002935) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002936 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002937}
2938
2939FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002940 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002942 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002943}
2944
2945FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002946 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002947) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002948 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949}
2950
2951FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002952 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002953) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002954 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002955}
2956
2957FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002958 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002959) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002960 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002961}
2962
2963PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002964 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002965) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002966 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002967}
2968
2969PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002970 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002971) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002972 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002973}
2974
2975IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002976 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002977) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002978 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002979}
2980
2981IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002982 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002983) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002984 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002985}
2986
2987BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002988 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002989) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002990 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002991}
2992
2993BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002994 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002995) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002996 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002997}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002998
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002999AddrSpaceCastInst::AddrSpaceCastInst(
3000 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3001) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3002 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3003}
3004
3005AddrSpaceCastInst::AddrSpaceCastInst(
3006 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3007) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3008 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3009}
3010
Chris Lattnerf16dc002006-09-17 19:29:56 +00003011//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003012// CmpInst Classes
3013//===----------------------------------------------------------------------===//
3014
Craig Topper3186c012012-09-23 02:12:10 +00003015void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003016
Chris Lattner229907c2011-07-18 04:54:35 +00003017CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003018 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003019 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003020 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003021 OperandTraits<CmpInst>::op_begin(this),
3022 OperandTraits<CmpInst>::operands(this),
3023 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003024 Op<0>() = LHS;
3025 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003026 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003027 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003028}
Gabor Greiff6caff662008-05-10 08:32:32 +00003029
Chris Lattner229907c2011-07-18 04:54:35 +00003030CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003031 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00003032 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003033 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003034 OperandTraits<CmpInst>::op_begin(this),
3035 OperandTraits<CmpInst>::operands(this),
3036 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003037 Op<0>() = LHS;
3038 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003039 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003040 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003041}
3042
3043CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003044CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003045 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003046 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003047 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003048 if (InsertBefore)
3049 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3050 S1, S2, Name);
3051 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003052 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003053 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003054 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003055
3056 if (InsertBefore)
3057 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3058 S1, S2, Name);
3059 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003060 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003061 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003062}
3063
3064CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003065CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003066 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003067 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003068 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3069 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003070 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003071 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3072 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003073}
3074
3075void CmpInst::swapOperands() {
3076 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3077 IC->swapOperands();
3078 else
3079 cast<FCmpInst>(this)->swapOperands();
3080}
3081
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003082bool CmpInst::isCommutative() const {
3083 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003084 return IC->isCommutative();
3085 return cast<FCmpInst>(this)->isCommutative();
3086}
3087
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003088bool CmpInst::isEquality() const {
3089 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003090 return IC->isEquality();
3091 return cast<FCmpInst>(this)->isEquality();
3092}
3093
3094
Dan Gohman4e724382008-05-31 02:47:54 +00003095CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003096 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003097 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003098 case ICMP_EQ: return ICMP_NE;
3099 case ICMP_NE: return ICMP_EQ;
3100 case ICMP_UGT: return ICMP_ULE;
3101 case ICMP_ULT: return ICMP_UGE;
3102 case ICMP_UGE: return ICMP_ULT;
3103 case ICMP_ULE: return ICMP_UGT;
3104 case ICMP_SGT: return ICMP_SLE;
3105 case ICMP_SLT: return ICMP_SGE;
3106 case ICMP_SGE: return ICMP_SLT;
3107 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003108
Dan Gohman4e724382008-05-31 02:47:54 +00003109 case FCMP_OEQ: return FCMP_UNE;
3110 case FCMP_ONE: return FCMP_UEQ;
3111 case FCMP_OGT: return FCMP_ULE;
3112 case FCMP_OLT: return FCMP_UGE;
3113 case FCMP_OGE: return FCMP_ULT;
3114 case FCMP_OLE: return FCMP_UGT;
3115 case FCMP_UEQ: return FCMP_ONE;
3116 case FCMP_UNE: return FCMP_OEQ;
3117 case FCMP_UGT: return FCMP_OLE;
3118 case FCMP_ULT: return FCMP_OGE;
3119 case FCMP_UGE: return FCMP_OLT;
3120 case FCMP_ULE: return FCMP_OGT;
3121 case FCMP_ORD: return FCMP_UNO;
3122 case FCMP_UNO: return FCMP_ORD;
3123 case FCMP_TRUE: return FCMP_FALSE;
3124 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003125 }
3126}
3127
Reid Spencer266e42b2006-12-23 06:05:41 +00003128ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3129 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003130 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003131 case ICMP_EQ: case ICMP_NE:
3132 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3133 return pred;
3134 case ICMP_UGT: return ICMP_SGT;
3135 case ICMP_ULT: return ICMP_SLT;
3136 case ICMP_UGE: return ICMP_SGE;
3137 case ICMP_ULE: return ICMP_SLE;
3138 }
3139}
3140
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003141ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3142 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003143 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003144 case ICMP_EQ: case ICMP_NE:
3145 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3146 return pred;
3147 case ICMP_SGT: return ICMP_UGT;
3148 case ICMP_SLT: return ICMP_ULT;
3149 case ICMP_SGE: return ICMP_UGE;
3150 case ICMP_SLE: return ICMP_ULE;
3151 }
3152}
3153
Reid Spencer0286bc12007-02-28 22:00:54 +00003154/// Initialize a set of values that all satisfy the condition with C.
3155///
3156ConstantRange
3157ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3158 APInt Lower(C);
3159 APInt Upper(C);
3160 uint32_t BitWidth = C.getBitWidth();
3161 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003162 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003163 case ICmpInst::ICMP_EQ: ++Upper; break;
3164 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003165 case ICmpInst::ICMP_ULT:
3166 Lower = APInt::getMinValue(BitWidth);
3167 // Check for an empty-set condition.
3168 if (Lower == Upper)
3169 return ConstantRange(BitWidth, /*isFullSet=*/false);
3170 break;
3171 case ICmpInst::ICMP_SLT:
3172 Lower = APInt::getSignedMinValue(BitWidth);
3173 // Check for an empty-set condition.
3174 if (Lower == Upper)
3175 return ConstantRange(BitWidth, /*isFullSet=*/false);
3176 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003177 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003178 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003179 // Check for an empty-set condition.
3180 if (Lower == Upper)
3181 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003182 break;
3183 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003184 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003185 // Check for an empty-set condition.
3186 if (Lower == Upper)
3187 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003188 break;
3189 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003190 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003191 // Check for a full-set condition.
3192 if (Lower == Upper)
3193 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003194 break;
3195 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003196 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003197 // Check for a full-set condition.
3198 if (Lower == Upper)
3199 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003200 break;
3201 case ICmpInst::ICMP_UGE:
3202 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003203 // Check for a full-set condition.
3204 if (Lower == Upper)
3205 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003206 break;
3207 case ICmpInst::ICMP_SGE:
3208 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003209 // Check for a full-set condition.
3210 if (Lower == Upper)
3211 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003212 break;
3213 }
3214 return ConstantRange(Lower, Upper);
3215}
3216
Dan Gohman4e724382008-05-31 02:47:54 +00003217CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003218 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003219 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003220 case ICMP_EQ: case ICMP_NE:
3221 return pred;
3222 case ICMP_SGT: return ICMP_SLT;
3223 case ICMP_SLT: return ICMP_SGT;
3224 case ICMP_SGE: return ICMP_SLE;
3225 case ICMP_SLE: return ICMP_SGE;
3226 case ICMP_UGT: return ICMP_ULT;
3227 case ICMP_ULT: return ICMP_UGT;
3228 case ICMP_UGE: return ICMP_ULE;
3229 case ICMP_ULE: return ICMP_UGE;
3230
Reid Spencerd9436b62006-11-20 01:22:35 +00003231 case FCMP_FALSE: case FCMP_TRUE:
3232 case FCMP_OEQ: case FCMP_ONE:
3233 case FCMP_UEQ: case FCMP_UNE:
3234 case FCMP_ORD: case FCMP_UNO:
3235 return pred;
3236 case FCMP_OGT: return FCMP_OLT;
3237 case FCMP_OLT: return FCMP_OGT;
3238 case FCMP_OGE: return FCMP_OLE;
3239 case FCMP_OLE: return FCMP_OGE;
3240 case FCMP_UGT: return FCMP_ULT;
3241 case FCMP_ULT: return FCMP_UGT;
3242 case FCMP_UGE: return FCMP_ULE;
3243 case FCMP_ULE: return FCMP_UGE;
3244 }
3245}
3246
Reid Spencer266e42b2006-12-23 06:05:41 +00003247bool CmpInst::isUnsigned(unsigned short predicate) {
3248 switch (predicate) {
3249 default: return false;
3250 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3251 case ICmpInst::ICMP_UGE: return true;
3252 }
3253}
3254
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003255bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003256 switch (predicate) {
3257 default: return false;
3258 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3259 case ICmpInst::ICMP_SGE: return true;
3260 }
3261}
3262
3263bool CmpInst::isOrdered(unsigned short predicate) {
3264 switch (predicate) {
3265 default: return false;
3266 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3267 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3268 case FCmpInst::FCMP_ORD: return true;
3269 }
3270}
3271
3272bool CmpInst::isUnordered(unsigned short predicate) {
3273 switch (predicate) {
3274 default: return false;
3275 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3276 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3277 case FCmpInst::FCMP_UNO: return true;
3278 }
3279}
3280
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003281bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3282 switch(predicate) {
3283 default: return false;
3284 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3285 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3286 }
3287}
3288
3289bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3290 switch(predicate) {
3291 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3292 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3293 default: return false;
3294 }
3295}
3296
3297
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003298//===----------------------------------------------------------------------===//
3299// SwitchInst Implementation
3300//===----------------------------------------------------------------------===//
3301
Chris Lattnerbaf00152010-11-17 05:41:46 +00003302void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3303 assert(Value && Default && NumReserved);
3304 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003305 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003306 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003307
Gabor Greif2d3024d2008-05-26 21:33:52 +00003308 OperandList[0] = Value;
3309 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003310}
3311
Chris Lattner2195fc42007-02-24 00:55:48 +00003312/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3313/// switch on and a default destination. The number of additional cases can
3314/// be specified here to make memory allocation more efficient. This
3315/// constructor can also autoinsert before another instruction.
3316SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3317 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003318 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003319 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003320 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003321}
3322
3323/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3324/// switch on and a default destination. The number of additional cases can
3325/// be specified here to make memory allocation more efficient. This
3326/// constructor also autoinserts at the end of the specified BasicBlock.
3327SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3328 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003329 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003330 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003331 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003332}
3333
Misha Brukmanb1c93172005-04-21 23:48:37 +00003334SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003335 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003336 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3337 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003338 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00003339 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003340 OL[i] = InOL[i];
3341 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003342 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003343 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003344}
3345
Gordon Henriksen14a55692007-12-10 02:14:30 +00003346SwitchInst::~SwitchInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003347 dropHungoffUses();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003348}
3349
3350
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003351/// addCase - Add an entry to the switch instruction...
3352///
Chris Lattner47ac1872005-02-24 05:32:09 +00003353void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003354 unsigned NewCaseIdx = getNumCases();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003355 unsigned OpNo = NumOperands;
3356 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003357 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003358 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003359 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003360 NumOperands = OpNo+2;
Bob Wilsone4077362013-09-09 19:14:35 +00003361 CaseIt Case(this, NewCaseIdx);
3362 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003363 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003364}
3365
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003366/// removeCase - This method removes the specified case and its successor
3367/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003368void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003369 unsigned idx = i.getCaseIndex();
3370
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003371 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003372
3373 unsigned NumOps = getNumOperands();
3374 Use *OL = OperandList;
3375
Jay Foad14277722011-02-01 09:22:34 +00003376 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003377 if (2 + (idx + 1) * 2 != NumOps) {
3378 OL[2 + idx * 2] = OL[NumOps - 2];
3379 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003380 }
3381
3382 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003383 OL[NumOps-2].set(nullptr);
3384 OL[NumOps-2+1].set(nullptr);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003385 NumOperands = NumOps-2;
3386}
3387
Jay Foade98f29d2011-04-01 08:00:58 +00003388/// growOperands - grow operands - This grows the operand list in response
3389/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003390///
Jay Foade98f29d2011-04-01 08:00:58 +00003391void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003392 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003393 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003394
3395 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003396 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003397 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003398 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003399 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003400 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003401 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003402 Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003403}
3404
3405
3406BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3407 return getSuccessor(idx);
3408}
3409unsigned SwitchInst::getNumSuccessorsV() const {
3410 return getNumSuccessors();
3411}
3412void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3413 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003414}
Chris Lattnerf22be932004-10-15 23:52:53 +00003415
Chris Lattner3ed871f2009-10-27 19:13:16 +00003416//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003417// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003418//===----------------------------------------------------------------------===//
3419
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003420void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003421 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003422 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003423 ReservedSpace = 1+NumDests;
3424 NumOperands = 1;
3425 OperandList = allocHungoffUses(ReservedSpace);
3426
3427 OperandList[0] = Address;
3428}
3429
3430
Jay Foade98f29d2011-04-01 08:00:58 +00003431/// growOperands - grow operands - This grows the operand list in response
3432/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003433///
Jay Foade98f29d2011-04-01 08:00:58 +00003434void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003435 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003436 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003437
3438 ReservedSpace = NumOps;
3439 Use *NewOps = allocHungoffUses(NumOps);
3440 Use *OldOps = OperandList;
3441 for (unsigned i = 0; i != e; ++i)
3442 NewOps[i] = OldOps[i];
3443 OperandList = NewOps;
Jay Foadbbb91f22011-01-16 15:30:52 +00003444 Use::zap(OldOps, OldOps + e, true);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003445}
3446
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003447IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3448 Instruction *InsertBefore)
3449: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003450 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003451 init(Address, NumCases);
3452}
3453
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003454IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3455 BasicBlock *InsertAtEnd)
3456: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003457 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003458 init(Address, NumCases);
3459}
3460
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003461IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3462 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003463 allocHungoffUses(IBI.getNumOperands()),
3464 IBI.getNumOperands()) {
3465 Use *OL = OperandList, *InOL = IBI.OperandList;
3466 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3467 OL[i] = InOL[i];
3468 SubclassOptionalData = IBI.SubclassOptionalData;
3469}
3470
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003471IndirectBrInst::~IndirectBrInst() {
Jay Foadbbb91f22011-01-16 15:30:52 +00003472 dropHungoffUses();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003473}
3474
3475/// addDestination - Add a destination.
3476///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003477void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003478 unsigned OpNo = NumOperands;
3479 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003480 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003481 // Initialize some new operands.
3482 assert(OpNo < ReservedSpace && "Growing didn't work!");
3483 NumOperands = OpNo+1;
3484 OperandList[OpNo] = DestBB;
3485}
3486
3487/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003488/// indirectbr instruction.
3489void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003490 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3491
3492 unsigned NumOps = getNumOperands();
3493 Use *OL = OperandList;
3494
3495 // Replace this value with the last one.
3496 OL[idx+1] = OL[NumOps-1];
3497
3498 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003499 OL[NumOps-1].set(nullptr);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003500 NumOperands = NumOps-1;
3501}
3502
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003503BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003504 return getSuccessor(idx);
3505}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003506unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003507 return getNumSuccessors();
3508}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003509void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003510 setSuccessor(idx, B);
3511}
3512
3513//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003514// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003515//===----------------------------------------------------------------------===//
3516
Chris Lattnerf22be932004-10-15 23:52:53 +00003517// Define these methods here so vtables don't get emitted into every translation
3518// unit that uses these classes.
3519
Devang Patel11cf3f42009-10-27 22:16:29 +00003520GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3521 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003522}
3523
Devang Patel11cf3f42009-10-27 22:16:29 +00003524BinaryOperator *BinaryOperator::clone_impl() const {
3525 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003526}
3527
Devang Patel11cf3f42009-10-27 22:16:29 +00003528FCmpInst* FCmpInst::clone_impl() const {
3529 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003530}
3531
Devang Patel11cf3f42009-10-27 22:16:29 +00003532ICmpInst* ICmpInst::clone_impl() const {
3533 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003534}
3535
Devang Patel11cf3f42009-10-27 22:16:29 +00003536ExtractValueInst *ExtractValueInst::clone_impl() const {
3537 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003538}
3539
Devang Patel11cf3f42009-10-27 22:16:29 +00003540InsertValueInst *InsertValueInst::clone_impl() const {
3541 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003542}
3543
Devang Patel11cf3f42009-10-27 22:16:29 +00003544AllocaInst *AllocaInst::clone_impl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003545 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3546 (Value *)getOperand(0), getAlignment());
3547 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3548 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003549}
3550
Devang Patel11cf3f42009-10-27 22:16:29 +00003551LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003552 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3553 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003554}
3555
Devang Patel11cf3f42009-10-27 22:16:29 +00003556StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003557 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003558 getAlignment(), getOrdering(), getSynchScope());
3559
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003560}
3561
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003562AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3563 AtomicCmpXchgInst *Result =
3564 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003565 getSuccessOrdering(), getFailureOrdering(),
3566 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003567 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003568 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003569 return Result;
3570}
3571
3572AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3573 AtomicRMWInst *Result =
3574 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3575 getOrdering(), getSynchScope());
3576 Result->setVolatile(isVolatile());
3577 return Result;
3578}
3579
Eli Friedmanfee02c62011-07-25 23:16:38 +00003580FenceInst *FenceInst::clone_impl() const {
3581 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3582}
3583
Devang Patel11cf3f42009-10-27 22:16:29 +00003584TruncInst *TruncInst::clone_impl() const {
3585 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003586}
3587
Devang Patel11cf3f42009-10-27 22:16:29 +00003588ZExtInst *ZExtInst::clone_impl() const {
3589 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003590}
3591
Devang Patel11cf3f42009-10-27 22:16:29 +00003592SExtInst *SExtInst::clone_impl() const {
3593 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003594}
3595
Devang Patel11cf3f42009-10-27 22:16:29 +00003596FPTruncInst *FPTruncInst::clone_impl() const {
3597 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003598}
3599
Devang Patel11cf3f42009-10-27 22:16:29 +00003600FPExtInst *FPExtInst::clone_impl() const {
3601 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003602}
3603
Devang Patel11cf3f42009-10-27 22:16:29 +00003604UIToFPInst *UIToFPInst::clone_impl() const {
3605 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003606}
3607
Devang Patel11cf3f42009-10-27 22:16:29 +00003608SIToFPInst *SIToFPInst::clone_impl() const {
3609 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003610}
3611
Devang Patel11cf3f42009-10-27 22:16:29 +00003612FPToUIInst *FPToUIInst::clone_impl() const {
3613 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003614}
3615
Devang Patel11cf3f42009-10-27 22:16:29 +00003616FPToSIInst *FPToSIInst::clone_impl() const {
3617 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003618}
3619
Devang Patel11cf3f42009-10-27 22:16:29 +00003620PtrToIntInst *PtrToIntInst::clone_impl() const {
3621 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003622}
3623
Devang Patel11cf3f42009-10-27 22:16:29 +00003624IntToPtrInst *IntToPtrInst::clone_impl() const {
3625 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003626}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003627
Devang Patel11cf3f42009-10-27 22:16:29 +00003628BitCastInst *BitCastInst::clone_impl() const {
3629 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003630}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003631
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003632AddrSpaceCastInst *AddrSpaceCastInst::clone_impl() const {
3633 return new AddrSpaceCastInst(getOperand(0), getType());
3634}
3635
Devang Patel11cf3f42009-10-27 22:16:29 +00003636CallInst *CallInst::clone_impl() const {
3637 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003638}
3639
Devang Patel11cf3f42009-10-27 22:16:29 +00003640SelectInst *SelectInst::clone_impl() const {
3641 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003642}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003643
Devang Patel11cf3f42009-10-27 22:16:29 +00003644VAArgInst *VAArgInst::clone_impl() const {
3645 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003646}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003647
Devang Patel11cf3f42009-10-27 22:16:29 +00003648ExtractElementInst *ExtractElementInst::clone_impl() const {
3649 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003650}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003651
Devang Patel11cf3f42009-10-27 22:16:29 +00003652InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003653 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003654}
3655
Devang Patel11cf3f42009-10-27 22:16:29 +00003656ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003657 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003658}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003659
Devang Patel11cf3f42009-10-27 22:16:29 +00003660PHINode *PHINode::clone_impl() const {
3661 return new PHINode(*this);
3662}
3663
Bill Wendlingfae14752011-08-12 20:24:12 +00003664LandingPadInst *LandingPadInst::clone_impl() const {
3665 return new LandingPadInst(*this);
3666}
3667
Devang Patel11cf3f42009-10-27 22:16:29 +00003668ReturnInst *ReturnInst::clone_impl() const {
3669 return new(getNumOperands()) ReturnInst(*this);
3670}
3671
3672BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003673 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003674}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003675
Devang Patel11cf3f42009-10-27 22:16:29 +00003676SwitchInst *SwitchInst::clone_impl() const {
3677 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003678}
3679
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003680IndirectBrInst *IndirectBrInst::clone_impl() const {
3681 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003682}
3683
3684
Devang Patel11cf3f42009-10-27 22:16:29 +00003685InvokeInst *InvokeInst::clone_impl() const {
3686 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003687}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003688
Bill Wendlingf891bf82011-07-31 06:30:59 +00003689ResumeInst *ResumeInst::clone_impl() const {
3690 return new(1) ResumeInst(*this);
3691}
3692
Devang Patel11cf3f42009-10-27 22:16:29 +00003693UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003694 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003695 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003696}