blob: 86c921aeda8af4a9f0101bcfba58a13816cde29f [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)
Pete Cooper3fc30402015-06-10 22:38:46 +000088 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
89 ReservedSpace(PN.getNumOperands()) {
90 allocHungoffUses(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
Chris Lattnerafdb3de2005-01-29 00:35:16 +000096// removeIncomingValue - Remove an incoming value. This is useful if a
97// predecessor basic block is deleted.
98Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +000099 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000100
101 // Move everything after this operand down.
102 //
103 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000104 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000106 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
107 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000108
109 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000110 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000111 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000112
113 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000114 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000116 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117 eraseFromParent();
118 }
119 return Removed;
120}
121
Jay Foade98f29d2011-04-01 08:00:58 +0000122/// growOperands - grow operands - This grows the operand list in response
123/// to a push_back style of operation. This grows the number of ops by 1.5
124/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000125///
Jay Foade98f29d2011-04-01 08:00:58 +0000126void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000127 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000128 unsigned NumOps = e + e / 2;
129 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
130
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000132 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000133}
134
Nate Begemanb3923212005-08-04 23:24:19 +0000135/// hasConstantValue - If the specified PHI node always merges together the same
136/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000137Value *PHINode::hasConstantValue() const {
138 // Exploit the fact that phi nodes always have at least one entry.
139 Value *ConstantValue = getIncomingValue(0);
140 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000141 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
142 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000143 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000144 // The case where the first value is this PHI.
145 ConstantValue = getIncomingValue(i);
146 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000147 if (ConstantValue == this)
148 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000149 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000150}
151
Bill Wendlingfae14752011-08-12 20:24:12 +0000152//===----------------------------------------------------------------------===//
153// LandingPadInst Implementation
154//===----------------------------------------------------------------------===//
155
David Majnemer7fddecc2015-06-17 20:52:32 +0000156LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
157 const Twine &NameStr, Instruction *InsertBefore)
158 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
159 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000160}
161
David Majnemer7fddecc2015-06-17 20:52:32 +0000162LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
163 const Twine &NameStr, BasicBlock *InsertAtEnd)
164 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
165 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000166}
167
168LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000169 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
170 LP.getNumOperands()),
171 ReservedSpace(LP.getNumOperands()) {
172 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000173 Use *OL = getOperandList();
174 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000175 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
176 OL[I] = InOL[I];
177
178 setCleanup(LP.isCleanup());
179}
180
David Majnemer7fddecc2015-06-17 20:52:32 +0000181LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000182 const Twine &NameStr,
183 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000184 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000185}
186
David Majnemer7fddecc2015-06-17 20:52:32 +0000187LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000188 const Twine &NameStr,
189 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000190 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000191}
192
David Majnemer7fddecc2015-06-17 20:52:32 +0000193void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000194 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000195 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000196 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000197 setName(NameStr);
198 setCleanup(false);
199}
200
201/// growOperands - grow operands - This grows the operand list in response to a
202/// push_back style of operation. This grows the number of ops by 2 times.
203void LandingPadInst::growOperands(unsigned Size) {
204 unsigned e = getNumOperands();
205 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000206 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000207 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000208}
209
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000210void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000211 unsigned OpNo = getNumOperands();
212 growOperands(1);
213 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000214 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000215 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000216}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000217
218//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000219// CallInst Implementation
220//===----------------------------------------------------------------------===//
221
Gordon Henriksen14a55692007-12-10 02:14:30 +0000222CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223}
224
David Blaikie348de692015-04-23 21:36:23 +0000225void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
226 const Twine &NameStr) {
227 this->FTy = FTy;
Pete Cooperb4eede22015-06-12 17:48:10 +0000228 assert(getNumOperands() == Args.size() + 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000229 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000230
Jay Foad5bd375a2011-07-15 08:37:34 +0000231#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000232 assert((Args.size() == FTy->getNumParams() ||
233 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000234 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000235
236 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000237 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000238 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000239 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000240#endif
241
242 std::copy(Args.begin(), Args.end(), op_begin());
243 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000244}
245
Jay Foad5bd375a2011-07-15 08:37:34 +0000246void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000247 FTy =
248 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000249 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000250 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000251
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000252 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000253
254 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255}
256
Daniel Dunbar4975db62009-07-25 04:41:11 +0000257CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258 Instruction *InsertBefore)
259 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
260 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000261 Instruction::Call,
262 OperandTraits<CallInst>::op_end(this) - 1,
263 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000264 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265}
266
Daniel Dunbar4975db62009-07-25 04:41:11 +0000267CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268 BasicBlock *InsertAtEnd)
269 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
270 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000271 Instruction::Call,
272 OperandTraits<CallInst>::op_end(this) - 1,
273 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000274 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000275}
276
Misha Brukmanb1c93172005-04-21 23:48:37 +0000277CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000278 : Instruction(CI.getType(), Instruction::Call,
279 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
280 CI.getNumOperands()),
281 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000282 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000283 setCallingConv(CI.getCallingConv());
284
Jay Foad5bd375a2011-07-15 08:37:34 +0000285 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000286 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287}
288
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000289void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000290 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000291 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000292 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000293}
294
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000295void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
296 AttributeSet PAL = getAttributes();
297 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
298 setAttributes(PAL);
299}
300
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000301void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000302 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000303 AttrBuilder B(attr);
304 LLVMContext &Context = getContext();
305 PAL = PAL.removeAttributes(Context, i,
306 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000307 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000308}
309
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000310void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
311 AttributeSet PAL = getAttributes();
312 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
313 setAttributes(PAL);
314}
315
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000316void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
317 AttributeSet PAL = getAttributes();
318 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
319 setAttributes(PAL);
320}
321
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000322bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000323 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000324 return true;
325 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000326 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000327 return false;
328}
329
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000330/// IsConstantOne - Return true only if val is constant int 1
331static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000332 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000333 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
334 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000335}
336
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000337static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000338 BasicBlock *InsertAtEnd, Type *IntPtrTy,
339 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000340 Value *ArraySize, Function *MallocF,
341 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000342 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000343 "createMalloc needs either InsertBefore or InsertAtEnd");
344
345 // malloc(type) becomes:
346 // bitcast (i8* malloc(typeSize)) to type*
347 // malloc(type, arraySize) becomes:
348 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000349 if (!ArraySize)
350 ArraySize = ConstantInt::get(IntPtrTy, 1);
351 else if (ArraySize->getType() != IntPtrTy) {
352 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000353 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
354 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000355 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000356 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
357 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000358 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000359
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000360 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000361 if (IsConstantOne(AllocSize)) {
362 AllocSize = ArraySize; // Operand * 1 = Operand
363 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
364 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
365 false /*ZExt*/);
366 // Malloc arg is constant product of type size and array size
367 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
368 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000369 // Multiply type size by the array size...
370 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000371 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
372 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000373 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000374 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
375 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000376 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000377 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000378
Victor Hernandez788eaab2009-09-18 19:20:02 +0000379 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000380 // Create the call to Malloc.
381 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
382 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000383 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000384 Value *MallocFunc = MallocF;
385 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000386 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000387 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000388 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000389 CallInst *MCall = nullptr;
390 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000391 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000392 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000393 Result = MCall;
394 if (Result->getType() != AllocPtrType)
395 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000396 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000397 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000398 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000399 Result = MCall;
400 if (Result->getType() != AllocPtrType) {
401 InsertAtEnd->getInstList().push_back(MCall);
402 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000403 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000404 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000405 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000407 if (Function *F = dyn_cast<Function>(MallocFunc)) {
408 MCall->setCallingConv(F->getCallingConv());
409 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
410 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000411 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000412
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000413 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000414}
415
416/// CreateMalloc - Generate the IR for a call to malloc:
417/// 1. Compute the malloc call's argument as the specified type's size,
418/// possibly multiplied by the array size if the array size is not
419/// constant 1.
420/// 2. Call malloc with that argument.
421/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000422Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000423 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000424 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000425 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000426 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000427 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000428 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000429}
430
431/// CreateMalloc - Generate the IR for a call to malloc:
432/// 1. Compute the malloc call's argument as the specified type's size,
433/// possibly multiplied by the array size if the array size is not
434/// constant 1.
435/// 2. Call malloc with that argument.
436/// 3. Bitcast the result of the malloc call to the specified type.
437/// Note: This function does not add the bitcast to the basic block, that is the
438/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000439Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000440 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000441 Value *AllocSize, Value *ArraySize,
442 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000443 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000444 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000446
Victor Hernandeze2971492009-10-24 04:23:03 +0000447static Instruction* createFree(Value* Source, Instruction *InsertBefore,
448 BasicBlock *InsertAtEnd) {
449 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
450 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000451 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000452 "Can not free something of nonpointer type!");
453
454 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
455 Module* M = BB->getParent()->getParent();
456
Chris Lattner229907c2011-07-18 04:54:35 +0000457 Type *VoidTy = Type::getVoidTy(M->getContext());
458 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000459 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000460 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000461 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000462 Value *PtrCast = Source;
463 if (InsertBefore) {
464 if (Source->getType() != IntPtrTy)
465 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
466 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
467 } else {
468 if (Source->getType() != IntPtrTy)
469 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
470 Result = CallInst::Create(FreeFunc, PtrCast, "");
471 }
472 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000473 if (Function *F = dyn_cast<Function>(FreeFunc))
474 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000475
476 return Result;
477}
478
479/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000480Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000481 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000482}
483
484/// CreateFree - Generate the IR for a call to the builtin free function.
485/// Note: This function does not add the call to the basic block, that is the
486/// responsibility of the caller.
487Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000488 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000489 assert(FreeCall && "CreateFree did not create a CallInst");
490 return FreeCall;
491}
492
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000493//===----------------------------------------------------------------------===//
494// InvokeInst Implementation
495//===----------------------------------------------------------------------===//
496
David Blaikie3e807092015-05-13 18:35:26 +0000497void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
498 BasicBlock *IfException, ArrayRef<Value *> Args,
499 const Twine &NameStr) {
500 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000501
Pete Cooperb4eede22015-06-12 17:48:10 +0000502 assert(getNumOperands() == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000503 Op<-3>() = Fn;
504 Op<-2>() = IfNormal;
505 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000506
507#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000508 assert(((Args.size() == FTy->getNumParams()) ||
509 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000510 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000511
Jay Foad5bd375a2011-07-15 08:37:34 +0000512 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000513 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000514 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000515 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000516#endif
517
518 std::copy(Args.begin(), Args.end(), op_begin());
519 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000520}
521
Misha Brukmanb1c93172005-04-21 23:48:37 +0000522InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000523 : TerminatorInst(II.getType(), Instruction::Invoke,
524 OperandTraits<InvokeInst>::op_end(this) -
525 II.getNumOperands(),
526 II.getNumOperands()),
527 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000528 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000529 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000530 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000531}
532
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000533BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
534 return getSuccessor(idx);
535}
536unsigned InvokeInst::getNumSuccessorsV() const {
537 return getNumSuccessors();
538}
539void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
540 return setSuccessor(idx, B);
541}
542
Michael Gottesman41748d72013-06-27 00:25:01 +0000543bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000544 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000545 return true;
546 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000547 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000548 return false;
549}
550
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000551bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000552 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000553 return true;
554 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000555 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000556 return false;
557}
558
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000559void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000560 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000561 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000562 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000563}
564
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000565void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000566 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000567 AttrBuilder B(attr);
568 PAL = PAL.removeAttributes(getContext(), i,
569 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000570 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000571}
572
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000573void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
574 AttributeSet PAL = getAttributes();
575 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
576 setAttributes(PAL);
577}
578
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000579void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
580 AttributeSet PAL = getAttributes();
581 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
582 setAttributes(PAL);
583}
584
Bill Wendlingfae14752011-08-12 20:24:12 +0000585LandingPadInst *InvokeInst::getLandingPadInst() const {
586 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
587}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000588
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000589//===----------------------------------------------------------------------===//
590// ReturnInst Implementation
591//===----------------------------------------------------------------------===//
592
Chris Lattner2195fc42007-02-24 00:55:48 +0000593ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000594 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000595 OperandTraits<ReturnInst>::op_end(this) -
596 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000597 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000598 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000599 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000600 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000601}
602
Owen Anderson55f1c092009-08-13 21:58:54 +0000603ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
604 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000605 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
606 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000607 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000608 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000609}
Owen Anderson55f1c092009-08-13 21:58:54 +0000610ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
611 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000612 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
613 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000614 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000615 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000616}
Owen Anderson55f1c092009-08-13 21:58:54 +0000617ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
618 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000619 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000620}
621
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000622unsigned ReturnInst::getNumSuccessorsV() const {
623 return getNumSuccessors();
624}
625
Devang Patelae682fb2008-02-26 17:56:20 +0000626/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
627/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000629 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000630}
631
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000632BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000633 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000634}
635
Devang Patel59643e52008-02-23 00:35:18 +0000636ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000637}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000639//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000640// ResumeInst Implementation
641//===----------------------------------------------------------------------===//
642
643ResumeInst::ResumeInst(const ResumeInst &RI)
644 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
645 OperandTraits<ResumeInst>::op_begin(this), 1) {
646 Op<0>() = RI.Op<0>();
647}
648
649ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
650 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
651 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
652 Op<0>() = Exn;
653}
654
655ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
656 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
657 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
658 Op<0>() = Exn;
659}
660
661unsigned ResumeInst::getNumSuccessorsV() const {
662 return getNumSuccessors();
663}
664
665void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
666 llvm_unreachable("ResumeInst has no successors!");
667}
668
669BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
670 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000671}
672
673//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000674// UnreachableInst Implementation
675//===----------------------------------------------------------------------===//
676
Owen Anderson55f1c092009-08-13 21:58:54 +0000677UnreachableInst::UnreachableInst(LLVMContext &Context,
678 Instruction *InsertBefore)
679 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000680 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000681}
Owen Anderson55f1c092009-08-13 21:58:54 +0000682UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
683 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000684 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000685}
686
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687unsigned UnreachableInst::getNumSuccessorsV() const {
688 return getNumSuccessors();
689}
690
691void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000692 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000693}
694
695BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000696 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000697}
698
699//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000700// BranchInst Implementation
701//===----------------------------------------------------------------------===//
702
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000703void BranchInst::AssertOK() {
704 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000705 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000707}
708
Chris Lattner2195fc42007-02-24 00:55:48 +0000709BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000710 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000711 OperandTraits<BranchInst>::op_end(this) - 1,
712 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000713 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000714 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000715}
716BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
717 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000718 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000719 OperandTraits<BranchInst>::op_end(this) - 3,
720 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000721 Op<-1>() = IfTrue;
722 Op<-2>() = IfFalse;
723 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000724#ifndef NDEBUG
725 AssertOK();
726#endif
727}
728
729BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000730 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000731 OperandTraits<BranchInst>::op_end(this) - 1,
732 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000733 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000734 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000735}
736
737BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
738 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000739 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000740 OperandTraits<BranchInst>::op_end(this) - 3,
741 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000742 Op<-1>() = IfTrue;
743 Op<-2>() = IfFalse;
744 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000745#ifndef NDEBUG
746 AssertOK();
747#endif
748}
749
750
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000751BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000752 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000753 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
754 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000755 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000756 if (BI.getNumOperands() != 1) {
757 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000758 Op<-3>() = BI.Op<-3>();
759 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000761 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000762}
763
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000764void BranchInst::swapSuccessors() {
765 assert(isConditional() &&
766 "Cannot swap successors of an unconditional branch");
767 Op<-1>().swap(Op<-2>());
768
769 // Update profile metadata if present and it matches our structural
770 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000771 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000772 if (!ProfileData || ProfileData->getNumOperands() != 3)
773 return;
774
775 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000776 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
777 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000778 setMetadata(LLVMContext::MD_prof,
779 MDNode::get(ProfileData->getContext(), Ops));
780}
781
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000782BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
783 return getSuccessor(idx);
784}
785unsigned BranchInst::getNumSuccessorsV() const {
786 return getNumSuccessors();
787}
788void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
789 setSuccessor(idx, B);
790}
791
792
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000793//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000794// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000795//===----------------------------------------------------------------------===//
796
Owen Andersonb6b25302009-07-14 23:09:55 +0000797static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000798 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000799 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000800 else {
801 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000802 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000803 assert(Amt->getType()->isIntegerTy() &&
804 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000805 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000806 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000807}
808
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000809AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
810 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000811
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000812AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
813 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000814
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000815AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000816 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000817 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000818
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000819AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000820 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000821 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000822
Chris Lattner229907c2011-07-18 04:54:35 +0000823AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000824 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +0000825 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
826 getAISize(Ty->getContext(), ArraySize), InsertBefore),
827 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000828 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000829 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000830 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000831}
832
Chris Lattner229907c2011-07-18 04:54:35 +0000833AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000834 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +0000835 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
836 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
837 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000838 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000839 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000840 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000841}
842
Gordon Henriksen14a55692007-12-10 02:14:30 +0000843// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000844AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000845}
846
Victor Hernandez8acf2952009-10-23 21:09:37 +0000847void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000848 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000849 assert(Align <= MaximumAlignment &&
850 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +0000851 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
852 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +0000853 assert(getAlignment() == Align && "Alignment representation error!");
854}
855
Victor Hernandez8acf2952009-10-23 21:09:37 +0000856bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000857 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000858 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000859 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000860}
861
Chris Lattner8b291e62008-11-26 02:54:17 +0000862/// isStaticAlloca - Return true if this alloca is in the entry block of the
863/// function and is a constant size. If so, the code generator will fold it
864/// into the prolog/epilog code, so it is basically free.
865bool AllocaInst::isStaticAlloca() const {
866 // Must be constant size.
867 if (!isa<ConstantInt>(getArraySize())) return false;
868
869 // Must be in the entry block.
870 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +0000871 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +0000872}
873
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000874//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000875// LoadInst Implementation
876//===----------------------------------------------------------------------===//
877
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000878void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000879 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000881 assert(!(isAtomic() && getAlignment() == 0) &&
882 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000883}
884
Daniel Dunbar4975db62009-07-25 04:41:11 +0000885LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000886 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000887
Daniel Dunbar4975db62009-07-25 04:41:11 +0000888LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000889 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000890
David Blaikie0c28fd72015-05-20 21:46:30 +0000891LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000892 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +0000893 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +0000894
895LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
896 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000897 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +0000898
David Blaikieb7a029872015-04-17 19:56:21 +0000899LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000900 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +0000901 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000902 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000903
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000904LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000905 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000906 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +0000907}
908
David Blaikie15d9a4c2015-04-06 20:59:48 +0000909LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +0000910 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +0000911 SynchronizationScope SynchScope, Instruction *InsertBef)
912 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +0000913 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +0000914 setVolatile(isVolatile);
915 setAlignment(Align);
916 setAtomic(Order, SynchScope);
917 AssertOK();
918 setName(Name);
919}
920
921LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
922 unsigned Align, AtomicOrdering Order,
923 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000924 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000925 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000926 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000927 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +0000928 setAlignment(Align);
929 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +0000930 AssertOK();
931 setName(Name);
932}
933
Daniel Dunbar27096822009-08-11 18:11:15 +0000934LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
935 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
936 Load, Ptr, InsertBef) {
937 setVolatile(false);
938 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000939 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000940 AssertOK();
941 if (Name && Name[0]) setName(Name);
942}
943
944LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
945 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
946 Load, Ptr, InsertAE) {
947 setVolatile(false);
948 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000949 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000950 AssertOK();
951 if (Name && Name[0]) setName(Name);
952}
953
David Blaikie0c28fd72015-05-20 21:46:30 +0000954LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +0000955 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +0000956 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
957 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +0000958 setVolatile(isVolatile);
959 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000960 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000961 AssertOK();
962 if (Name && Name[0]) setName(Name);
963}
964
965LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
966 BasicBlock *InsertAE)
967 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
968 Load, Ptr, InsertAE) {
969 setVolatile(isVolatile);
970 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000971 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000972 AssertOK();
973 if (Name && Name[0]) setName(Name);
974}
975
Christopher Lamb84485702007-04-22 19:24:39 +0000976void LoadInst::setAlignment(unsigned Align) {
977 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000978 assert(Align <= MaximumAlignment &&
979 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +0000980 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000981 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000982 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000983}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000984
985//===----------------------------------------------------------------------===//
986// StoreInst Implementation
987//===----------------------------------------------------------------------===//
988
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000989void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000990 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +0000991 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000992 "Ptr must have pointer type!");
993 assert(getOperand(0)->getType() ==
994 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000995 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +0000996 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +0000997 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000998}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000999
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001000StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001001 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001002
1003StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001004 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001005
Misha Brukmanb1c93172005-04-21 23:48:37 +00001006StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001007 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001008 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001009
1010StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001011 BasicBlock *InsertAtEnd)
1012 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1013
1014StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1015 Instruction *InsertBefore)
1016 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1017 InsertBefore) {}
1018
1019StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1020 BasicBlock *InsertAtEnd)
1021 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1022 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001023
Misha Brukmanb1c93172005-04-21 23:48:37 +00001024StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001025 unsigned Align, AtomicOrdering Order,
1026 SynchronizationScope SynchScope,
1027 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001028 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001029 OperandTraits<StoreInst>::op_begin(this),
1030 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001031 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001032 Op<0>() = val;
1033 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001034 setVolatile(isVolatile);
1035 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001036 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001037 AssertOK();
1038}
1039
1040StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001041 unsigned Align, AtomicOrdering Order,
1042 SynchronizationScope SynchScope,
1043 BasicBlock *InsertAtEnd)
1044 : Instruction(Type::getVoidTy(val->getContext()), Store,
1045 OperandTraits<StoreInst>::op_begin(this),
1046 OperandTraits<StoreInst>::operands(this),
1047 InsertAtEnd) {
1048 Op<0>() = val;
1049 Op<1>() = addr;
1050 setVolatile(isVolatile);
1051 setAlignment(Align);
1052 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001053 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001054}
1055
Christopher Lamb84485702007-04-22 19:24:39 +00001056void StoreInst::setAlignment(unsigned Align) {
1057 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001058 assert(Align <= MaximumAlignment &&
1059 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001060 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001061 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001062 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001063}
1064
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001065//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001066// AtomicCmpXchgInst Implementation
1067//===----------------------------------------------------------------------===//
1068
1069void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001070 AtomicOrdering SuccessOrdering,
1071 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001072 SynchronizationScope SynchScope) {
1073 Op<0>() = Ptr;
1074 Op<1>() = Cmp;
1075 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001076 setSuccessOrdering(SuccessOrdering);
1077 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001078 setSynchScope(SynchScope);
1079
1080 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1081 "All operands must be non-null!");
1082 assert(getOperand(0)->getType()->isPointerTy() &&
1083 "Ptr must have pointer type!");
1084 assert(getOperand(1)->getType() ==
1085 cast<PointerType>(getOperand(0)->getType())->getElementType()
1086 && "Ptr must be a pointer to Cmp type!");
1087 assert(getOperand(2)->getType() ==
1088 cast<PointerType>(getOperand(0)->getType())->getElementType()
1089 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001090 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001091 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001092 assert(FailureOrdering != NotAtomic &&
1093 "AtomicCmpXchg instructions must be atomic!");
1094 assert(SuccessOrdering >= FailureOrdering &&
1095 "AtomicCmpXchg success ordering must be at least as strong as fail");
1096 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1097 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001098}
1099
1100AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001101 AtomicOrdering SuccessOrdering,
1102 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001103 SynchronizationScope SynchScope,
1104 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001105 : Instruction(
1106 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1107 nullptr),
1108 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1109 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001110 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001111}
1112
1113AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001114 AtomicOrdering SuccessOrdering,
1115 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001116 SynchronizationScope SynchScope,
1117 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001118 : Instruction(
1119 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1120 nullptr),
1121 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1122 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001123 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001124}
Tim Northover420a2162014-06-13 14:24:07 +00001125
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001126//===----------------------------------------------------------------------===//
1127// AtomicRMWInst Implementation
1128//===----------------------------------------------------------------------===//
1129
1130void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1131 AtomicOrdering Ordering,
1132 SynchronizationScope SynchScope) {
1133 Op<0>() = Ptr;
1134 Op<1>() = Val;
1135 setOperation(Operation);
1136 setOrdering(Ordering);
1137 setSynchScope(SynchScope);
1138
1139 assert(getOperand(0) && getOperand(1) &&
1140 "All operands must be non-null!");
1141 assert(getOperand(0)->getType()->isPointerTy() &&
1142 "Ptr must have pointer type!");
1143 assert(getOperand(1)->getType() ==
1144 cast<PointerType>(getOperand(0)->getType())->getElementType()
1145 && "Ptr must be a pointer to Val type!");
1146 assert(Ordering != NotAtomic &&
1147 "AtomicRMW instructions must be atomic!");
1148}
1149
1150AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1151 AtomicOrdering Ordering,
1152 SynchronizationScope SynchScope,
1153 Instruction *InsertBefore)
1154 : Instruction(Val->getType(), AtomicRMW,
1155 OperandTraits<AtomicRMWInst>::op_begin(this),
1156 OperandTraits<AtomicRMWInst>::operands(this),
1157 InsertBefore) {
1158 Init(Operation, Ptr, Val, Ordering, SynchScope);
1159}
1160
1161AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1162 AtomicOrdering Ordering,
1163 SynchronizationScope SynchScope,
1164 BasicBlock *InsertAtEnd)
1165 : Instruction(Val->getType(), AtomicRMW,
1166 OperandTraits<AtomicRMWInst>::op_begin(this),
1167 OperandTraits<AtomicRMWInst>::operands(this),
1168 InsertAtEnd) {
1169 Init(Operation, Ptr, Val, Ordering, SynchScope);
1170}
1171
1172//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001173// FenceInst Implementation
1174//===----------------------------------------------------------------------===//
1175
1176FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1177 SynchronizationScope SynchScope,
1178 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001179 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001180 setOrdering(Ordering);
1181 setSynchScope(SynchScope);
1182}
1183
1184FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1185 SynchronizationScope SynchScope,
1186 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001187 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001188 setOrdering(Ordering);
1189 setSynchScope(SynchScope);
1190}
1191
1192//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001193// GetElementPtrInst Implementation
1194//===----------------------------------------------------------------------===//
1195
Jay Foadd1b78492011-07-25 09:48:08 +00001196void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001197 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001198 assert(getNumOperands() == 1 + IdxList.size() &&
1199 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001200 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001201 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001202 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001203}
1204
Gabor Greiff6caff662008-05-10 08:32:32 +00001205GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001206 : Instruction(GEPI.getType(), GetElementPtr,
1207 OperandTraits<GetElementPtrInst>::op_end(this) -
1208 GEPI.getNumOperands(),
1209 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001210 SourceElementType(GEPI.SourceElementType),
1211 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001212 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001213 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001214}
1215
Chris Lattner6090a422009-03-09 04:46:40 +00001216/// getIndexedType - Returns the type of the element that would be accessed with
1217/// a gep instruction with the specified parameters.
1218///
1219/// The Idxs pointer should point to a continuous piece of memory containing the
1220/// indices, either as Value* or uint64_t.
1221///
1222/// A null type is returned if the indices are invalid for the specified
1223/// pointer type.
1224///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001225template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001226static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001227 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001228 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001229 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001230
Chris Lattner6090a422009-03-09 04:46:40 +00001231 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001232 // it cannot be 'stepped over'.
1233 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001234 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001235
Dan Gohman1ecaf452008-05-31 00:58:22 +00001236 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001237 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001238 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001239 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001240 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001241 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001242 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001243 }
Craig Topperc6207612014-04-09 06:08:46 +00001244 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001245}
1246
David Blaikied288fb82015-03-30 21:41:43 +00001247Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1248 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001249}
1250
David Blaikied288fb82015-03-30 21:41:43 +00001251Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001252 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001253 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001254}
1255
David Blaikied288fb82015-03-30 21:41:43 +00001256Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1257 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001258}
1259
Chris Lattner45f15572007-04-14 00:12:57 +00001260/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1261/// zeros. If so, the result pointer and the first operand have the same
1262/// value, just potentially different types.
1263bool GetElementPtrInst::hasAllZeroIndices() const {
1264 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1265 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1266 if (!CI->isZero()) return false;
1267 } else {
1268 return false;
1269 }
1270 }
1271 return true;
1272}
1273
Chris Lattner27058292007-04-27 20:35:56 +00001274/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1275/// constant integers. If so, the result pointer and the first operand have
1276/// a constant offset between them.
1277bool GetElementPtrInst::hasAllConstantIndices() const {
1278 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1279 if (!isa<ConstantInt>(getOperand(i)))
1280 return false;
1281 }
1282 return true;
1283}
1284
Dan Gohman1b849082009-09-07 23:54:19 +00001285void GetElementPtrInst::setIsInBounds(bool B) {
1286 cast<GEPOperator>(this)->setIsInBounds(B);
1287}
Chris Lattner45f15572007-04-14 00:12:57 +00001288
Nick Lewycky28a5f252009-09-27 21:33:04 +00001289bool GetElementPtrInst::isInBounds() const {
1290 return cast<GEPOperator>(this)->isInBounds();
1291}
1292
Chandler Carruth1e140532012-12-11 10:29:10 +00001293bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1294 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001295 // Delegate to the generic GEPOperator implementation.
1296 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001297}
1298
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001299//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001300// ExtractElementInst Implementation
1301//===----------------------------------------------------------------------===//
1302
1303ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001304 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001305 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001306 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001307 ExtractElement,
1308 OperandTraits<ExtractElementInst>::op_begin(this),
1309 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001310 assert(isValidOperands(Val, Index) &&
1311 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001312 Op<0>() = Val;
1313 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001314 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001315}
1316
1317ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001318 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001319 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001320 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001321 ExtractElement,
1322 OperandTraits<ExtractElementInst>::op_begin(this),
1323 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001324 assert(isValidOperands(Val, Index) &&
1325 "Invalid extractelement instruction operands!");
1326
Gabor Greif2d3024d2008-05-26 21:33:52 +00001327 Op<0>() = Val;
1328 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001329 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001330}
1331
Chris Lattner65511ff2006-10-05 06:24:58 +00001332
Chris Lattner54865b32006-04-08 04:05:48 +00001333bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001334 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001335 return false;
1336 return true;
1337}
1338
1339
Robert Bocchino23004482006-01-10 19:05:34 +00001340//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001341// InsertElementInst Implementation
1342//===----------------------------------------------------------------------===//
1343
Chris Lattner54865b32006-04-08 04:05:48 +00001344InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001345 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001346 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001347 : Instruction(Vec->getType(), InsertElement,
1348 OperandTraits<InsertElementInst>::op_begin(this),
1349 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001350 assert(isValidOperands(Vec, Elt, Index) &&
1351 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001352 Op<0>() = Vec;
1353 Op<1>() = Elt;
1354 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001355 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001356}
1357
Chris Lattner54865b32006-04-08 04:05:48 +00001358InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001359 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001360 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001361 : Instruction(Vec->getType(), InsertElement,
1362 OperandTraits<InsertElementInst>::op_begin(this),
1363 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001364 assert(isValidOperands(Vec, Elt, Index) &&
1365 "Invalid insertelement instruction operands!");
1366
Gabor Greif2d3024d2008-05-26 21:33:52 +00001367 Op<0>() = Vec;
1368 Op<1>() = Elt;
1369 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001370 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001371}
1372
Chris Lattner54865b32006-04-08 04:05:48 +00001373bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1374 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001375 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001376 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001377
Reid Spencerd84d35b2007-02-15 02:26:10 +00001378 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001379 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001380
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001381 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001382 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001383 return true;
1384}
1385
1386
Robert Bocchinoca27f032006-01-17 20:07:22 +00001387//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001388// ShuffleVectorInst Implementation
1389//===----------------------------------------------------------------------===//
1390
1391ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001392 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001393 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001394: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001395 cast<VectorType>(Mask->getType())->getNumElements()),
1396 ShuffleVector,
1397 OperandTraits<ShuffleVectorInst>::op_begin(this),
1398 OperandTraits<ShuffleVectorInst>::operands(this),
1399 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001400 assert(isValidOperands(V1, V2, Mask) &&
1401 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001402 Op<0>() = V1;
1403 Op<1>() = V2;
1404 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001405 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001406}
1407
1408ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001409 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001410 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001411: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1412 cast<VectorType>(Mask->getType())->getNumElements()),
1413 ShuffleVector,
1414 OperandTraits<ShuffleVectorInst>::op_begin(this),
1415 OperandTraits<ShuffleVectorInst>::operands(this),
1416 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001417 assert(isValidOperands(V1, V2, Mask) &&
1418 "Invalid shuffle vector instruction operands!");
1419
Gabor Greif2d3024d2008-05-26 21:33:52 +00001420 Op<0>() = V1;
1421 Op<1>() = V2;
1422 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001423 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001424}
1425
Mon P Wang25f01062008-11-10 04:46:22 +00001426bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001427 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001428 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001429 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001430 return false;
1431
Chris Lattner1dcb6542012-01-25 23:49:49 +00001432 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001433 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001434 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001435 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001436
1437 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001438 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1439 return true;
1440
Nate Begeman60a31c32010-08-13 00:16:46 +00001441 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001442 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001443 for (Value *Op : MV->operands()) {
1444 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001445 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001446 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001447 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001448 return false;
1449 }
1450 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001451 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001452 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001453
1454 if (const ConstantDataSequential *CDS =
1455 dyn_cast<ConstantDataSequential>(Mask)) {
1456 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1457 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1458 if (CDS->getElementAsInteger(i) >= V1Size*2)
1459 return false;
1460 return true;
1461 }
1462
1463 // The bitcode reader can create a place holder for a forward reference
1464 // used as the shuffle mask. When this occurs, the shuffle mask will
1465 // fall into this case and fail. To avoid this error, do this bit of
1466 // ugliness to allow such a mask pass.
1467 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1468 if (CE->getOpcode() == Instruction::UserOp1)
1469 return true;
1470
1471 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001472}
1473
Chris Lattnerf724e342008-03-02 05:28:33 +00001474/// getMaskValue - Return the index from the shuffle mask for the specified
1475/// output result. This is either -1 if the element is undef or a number less
1476/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001477int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1478 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1479 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001480 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001481 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001482 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001483 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001484 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001485}
1486
Chris Lattner1dcb6542012-01-25 23:49:49 +00001487/// getShuffleMask - Return the full mask for this instruction, where each
1488/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001489void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1490 SmallVectorImpl<int> &Result) {
1491 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001492
Chris Lattnercf129702012-01-26 02:51:13 +00001493 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001494 for (unsigned i = 0; i != NumElts; ++i)
1495 Result.push_back(CDS->getElementAsInteger(i));
1496 return;
1497 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001498 for (unsigned i = 0; i != NumElts; ++i) {
1499 Constant *C = Mask->getAggregateElement(i);
1500 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001501 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001502 }
1503}
1504
1505
Dan Gohman12fce772008-05-15 19:50:34 +00001506//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001507// InsertValueInst Class
1508//===----------------------------------------------------------------------===//
1509
Jay Foad57aa6362011-07-13 10:26:04 +00001510void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1511 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001512 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001513
1514 // There's no fundamental reason why we require at least one index
1515 // (other than weirdness with &*IdxBegin being invalid; see
1516 // getelementptr's init routine for example). But there's no
1517 // present need to support it.
1518 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1519
1520 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001521 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001522 Op<0>() = Agg;
1523 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001524
Jay Foad57aa6362011-07-13 10:26:04 +00001525 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001526 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001527}
1528
1529InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001530 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001531 OperandTraits<InsertValueInst>::op_begin(this), 2),
1532 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001533 Op<0>() = IVI.getOperand(0);
1534 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001535 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001536}
1537
1538//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001539// ExtractValueInst Class
1540//===----------------------------------------------------------------------===//
1541
Jay Foad57aa6362011-07-13 10:26:04 +00001542void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001543 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001544
Jay Foad57aa6362011-07-13 10:26:04 +00001545 // There's no fundamental reason why we require at least one index.
1546 // But there's no present need to support it.
1547 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001548
Jay Foad57aa6362011-07-13 10:26:04 +00001549 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001550 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001551}
1552
1553ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001554 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001555 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001556 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001557}
1558
Dan Gohman12fce772008-05-15 19:50:34 +00001559// getIndexedType - Returns the type of the element that would be extracted
1560// with an extractvalue instruction with the specified parameters.
1561//
1562// A null type is returned if the indices are invalid for the specified
1563// pointer type.
1564//
Chris Lattner229907c2011-07-18 04:54:35 +00001565Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001566 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001567 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001568 // We can't use CompositeType::indexValid(Index) here.
1569 // indexValid() always returns true for arrays because getelementptr allows
1570 // out-of-bounds indices. Since we don't allow those for extractvalue and
1571 // insertvalue we need to check array indexing manually.
1572 // Since the only other types we can index into are struct types it's just
1573 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001574 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001575 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001576 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001577 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001578 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001579 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001580 } else {
1581 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001582 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001583 }
1584
1585 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001586 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001587 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001588}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001589
1590//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001591// BinaryOperator Class
1592//===----------------------------------------------------------------------===//
1593
Chris Lattner2195fc42007-02-24 00:55:48 +00001594BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001595 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001596 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001597 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001598 OperandTraits<BinaryOperator>::op_begin(this),
1599 OperandTraits<BinaryOperator>::operands(this),
1600 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001601 Op<0>() = S1;
1602 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001603 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001604 setName(Name);
1605}
1606
1607BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001608 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001609 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001610 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001611 OperandTraits<BinaryOperator>::op_begin(this),
1612 OperandTraits<BinaryOperator>::operands(this),
1613 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001614 Op<0>() = S1;
1615 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001616 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001617 setName(Name);
1618}
1619
1620
1621void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001622 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001623 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001624 assert(LHS->getType() == RHS->getType() &&
1625 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001626#ifndef NDEBUG
1627 switch (iType) {
1628 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001629 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001630 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001631 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001632 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001633 "Tried to create an integer operation on a non-integer type!");
1634 break;
1635 case FAdd: case FSub:
1636 case FMul:
1637 assert(getType() == LHS->getType() &&
1638 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001639 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001640 "Tried to create a floating-point operation on a "
1641 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001642 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001643 case UDiv:
1644 case SDiv:
1645 assert(getType() == LHS->getType() &&
1646 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001647 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001648 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001649 "Incorrect operand type (not integer) for S/UDIV");
1650 break;
1651 case FDiv:
1652 assert(getType() == LHS->getType() &&
1653 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001654 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001655 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001656 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001657 case URem:
1658 case SRem:
1659 assert(getType() == LHS->getType() &&
1660 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001661 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001662 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001663 "Incorrect operand type (not integer) for S/UREM");
1664 break;
1665 case FRem:
1666 assert(getType() == LHS->getType() &&
1667 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001668 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001669 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001670 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001671 case Shl:
1672 case LShr:
1673 case AShr:
1674 assert(getType() == LHS->getType() &&
1675 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001676 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001677 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001678 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001679 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001680 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001681 case And: case Or:
1682 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001683 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001684 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001685 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001686 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001687 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001688 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001690 default:
1691 break;
1692 }
1693#endif
1694}
1695
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001696BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001697 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001698 Instruction *InsertBefore) {
1699 assert(S1->getType() == S2->getType() &&
1700 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001701 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001702}
1703
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001704BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001705 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001706 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001707 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001708 InsertAtEnd->getInstList().push_back(Res);
1709 return Res;
1710}
1711
Dan Gohman5476cfd2009-08-12 16:23:25 +00001712BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001713 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001714 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001715 return new BinaryOperator(Instruction::Sub,
1716 zero, Op,
1717 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001718}
1719
Dan Gohman5476cfd2009-08-12 16:23:25 +00001720BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001721 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001722 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001723 return new BinaryOperator(Instruction::Sub,
1724 zero, Op,
1725 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001726}
1727
Dan Gohman4ab44202009-12-18 02:58:50 +00001728BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1729 Instruction *InsertBefore) {
1730 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1731 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1732}
1733
1734BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1735 BasicBlock *InsertAtEnd) {
1736 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1737 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1738}
1739
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001740BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1741 Instruction *InsertBefore) {
1742 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1743 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1744}
1745
1746BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1747 BasicBlock *InsertAtEnd) {
1748 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1749 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1750}
1751
Dan Gohman5476cfd2009-08-12 16:23:25 +00001752BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001753 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001754 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001755 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001756 Op->getType(), Name, InsertBefore);
1757}
1758
Dan Gohman5476cfd2009-08-12 16:23:25 +00001759BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001760 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001761 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001762 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001763 Op->getType(), Name, InsertAtEnd);
1764}
1765
Dan Gohman5476cfd2009-08-12 16:23:25 +00001766BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001767 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001768 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001769 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001770 Op->getType(), Name, InsertBefore);
1771}
1772
Dan Gohman5476cfd2009-08-12 16:23:25 +00001773BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001774 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001775 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001776 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001777 Op->getType(), Name, InsertAtEnd);
1778}
1779
1780
1781// isConstantAllOnes - Helper function for several functions below
1782static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001783 if (const Constant *C = dyn_cast<Constant>(V))
1784 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001785 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001786}
1787
Owen Andersonbb2501b2009-07-13 22:18:28 +00001788bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001789 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1790 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001791 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1792 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001793 return false;
1794}
1795
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001796bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001797 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1798 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001799 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
1800 if (!IgnoreZeroSign)
1801 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
1802 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
1803 }
Dan Gohmana5b96452009-06-04 22:49:04 +00001804 return false;
1805}
1806
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001807bool BinaryOperator::isNot(const Value *V) {
1808 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1809 return (Bop->getOpcode() == Instruction::Xor &&
1810 (isConstantAllOnes(Bop->getOperand(1)) ||
1811 isConstantAllOnes(Bop->getOperand(0))));
1812 return false;
1813}
1814
Chris Lattner2c7d1772005-04-24 07:28:37 +00001815Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001816 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001817}
1818
Chris Lattner2c7d1772005-04-24 07:28:37 +00001819const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1820 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001821}
1822
Dan Gohmana5b96452009-06-04 22:49:04 +00001823Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001824 return cast<BinaryOperator>(BinOp)->getOperand(1);
1825}
1826
1827const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1828 return getFNegArgument(const_cast<Value*>(BinOp));
1829}
1830
Chris Lattner2c7d1772005-04-24 07:28:37 +00001831Value *BinaryOperator::getNotArgument(Value *BinOp) {
1832 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1833 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1834 Value *Op0 = BO->getOperand(0);
1835 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001836 if (isConstantAllOnes(Op0)) return Op1;
1837
1838 assert(isConstantAllOnes(Op1));
1839 return Op0;
1840}
1841
Chris Lattner2c7d1772005-04-24 07:28:37 +00001842const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1843 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001844}
1845
1846
1847// swapOperands - Exchange the two operands to this instruction. This
1848// instruction is safe to use on any binary instruction and does not
1849// modify the semantics of the instruction. If the instruction is
1850// order dependent (SetLT f.e.) the opcode is changed.
1851//
1852bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001853 if (!isCommutative())
1854 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001855 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001856 return false;
1857}
1858
Dan Gohman1b849082009-09-07 23:54:19 +00001859void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1860 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1861}
1862
1863void BinaryOperator::setHasNoSignedWrap(bool b) {
1864 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1865}
1866
1867void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001868 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001869}
1870
Nick Lewycky28a5f252009-09-27 21:33:04 +00001871bool BinaryOperator::hasNoUnsignedWrap() const {
1872 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1873}
1874
1875bool BinaryOperator::hasNoSignedWrap() const {
1876 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1877}
1878
1879bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001880 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001881}
1882
Sanjay Patela982d992014-09-03 01:06:50 +00001883void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00001884 // Copy the wrapping flags.
1885 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
1886 setHasNoSignedWrap(OB->hasNoSignedWrap());
1887 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
1888 }
1889
1890 // Copy the exact flag.
1891 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
1892 setIsExact(PE->isExact());
1893
1894 // Copy the fast-math flags.
1895 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00001896 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00001897}
1898
Sanjay Patela982d992014-09-03 01:06:50 +00001899void BinaryOperator::andIRFlags(const Value *V) {
1900 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
1901 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
1902 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
1903 }
1904
1905 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
1906 setIsExact(isExact() & PE->isExact());
1907
1908 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
1909 FastMathFlags FM = getFastMathFlags();
1910 FM &= FP->getFastMathFlags();
1911 copyFastMathFlags(FM);
1912 }
1913}
1914
1915
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001916//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00001917// FPMathOperator Class
1918//===----------------------------------------------------------------------===//
1919
1920/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
1921/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00001922/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00001923float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001924 const MDNode *MD =
1925 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00001926 if (!MD)
1927 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001928 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00001929 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00001930}
1931
1932
1933//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001934// CastInst Class
1935//===----------------------------------------------------------------------===//
1936
David Blaikie3a15e142011-12-01 08:00:17 +00001937void CastInst::anchor() {}
1938
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001939// Just determine if this cast only deals with integral->integral conversion.
1940bool CastInst::isIntegerCast() const {
1941 switch (getOpcode()) {
1942 default: return false;
1943 case Instruction::ZExt:
1944 case Instruction::SExt:
1945 case Instruction::Trunc:
1946 return true;
1947 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001948 return getOperand(0)->getType()->isIntegerTy() &&
1949 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001950 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001951}
1952
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001953bool CastInst::isLosslessCast() const {
1954 // Only BitCast can be lossless, exit fast if we're not BitCast
1955 if (getOpcode() != Instruction::BitCast)
1956 return false;
1957
1958 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00001959 Type* SrcTy = getOperand(0)->getType();
1960 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001961 if (SrcTy == DstTy)
1962 return true;
1963
Reid Spencer8d9336d2006-12-31 05:26:44 +00001964 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001965 if (SrcTy->isPointerTy())
1966 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001967 return false; // Other types have no identity values
1968}
1969
1970/// This function determines if the CastInst does not require any bits to be
1971/// changed in order to effect the cast. Essentially, it identifies cases where
1972/// no code gen is necessary for the cast, hence the name no-op cast. For
1973/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001974/// # bitcast i32* %x to i8*
1975/// # bitcast <2 x i32> %x to <4 x i16>
1976/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001977/// @brief Determine if the described cast is a no-op.
1978bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00001979 Type *SrcTy,
1980 Type *DestTy,
1981 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001982 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001983 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001984 case Instruction::Trunc:
1985 case Instruction::ZExt:
1986 case Instruction::SExt:
1987 case Instruction::FPTrunc:
1988 case Instruction::FPExt:
1989 case Instruction::UIToFP:
1990 case Instruction::SIToFP:
1991 case Instruction::FPToUI:
1992 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001993 case Instruction::AddrSpaceCast:
1994 // TODO: Target informations may give a more accurate answer here.
1995 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001996 case Instruction::BitCast:
1997 return true; // BitCast never modifies bits.
1998 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001999 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002000 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002001 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002002 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002003 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002004 }
2005}
2006
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002007/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002008bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002009 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2010}
2011
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002012bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002013 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002014 if (getOpcode() == Instruction::PtrToInt)
2015 PtrOpTy = getOperand(0)->getType();
2016 else if (getOpcode() == Instruction::IntToPtr)
2017 PtrOpTy = getType();
2018
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002019 Type *IntPtrTy =
2020 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002021
2022 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2023}
2024
2025/// This function determines if a pair of casts can be eliminated and what
2026/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002027/// instructions like this:
2028/// * %F = firstOpcode SrcTy %x to MidTy
2029/// * %S = secondOpcode MidTy %F to DstTy
2030/// The function returns a resultOpcode so these two casts can be replaced with:
2031/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2032/// If no such cast is permited, the function returns 0.
2033unsigned CastInst::isEliminableCastPair(
2034 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002035 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2036 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002037 // Define the 144 possibilities for these two cast instructions. The values
2038 // in this matrix determine what to do in a given situation and select the
2039 // case in the switch below. The rows correspond to firstOp, the columns
2040 // correspond to secondOp. In looking at the table below, keep in mind
2041 // the following cast properties:
2042 //
2043 // Size Compare Source Destination
2044 // Operator Src ? Size Type Sign Type Sign
2045 // -------- ------------ ------------------- ---------------------
2046 // TRUNC > Integer Any Integral Any
2047 // ZEXT < Integral Unsigned Integer Any
2048 // SEXT < Integral Signed Integer Any
2049 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002050 // FPTOSI n/a FloatPt n/a Integral Signed
2051 // UITOFP n/a Integral Unsigned FloatPt n/a
2052 // SITOFP n/a Integral Signed FloatPt n/a
2053 // FPTRUNC > FloatPt n/a FloatPt n/a
2054 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002055 // PTRTOINT n/a Pointer n/a Integral Unsigned
2056 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002057 // BITCAST = FirstClass n/a FirstClass n/a
2058 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002059 //
2060 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002061 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2062 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002063 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002064 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002065 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002066 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002067 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2069 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002070 // T F F U S F F P I B A -+
2071 // R Z S P P I I T P 2 N T S |
2072 // U E E 2 2 2 2 R E I T C C +- secondOp
2073 // N X X U S F F N X N 2 V V |
2074 // C T T I I P P C T T P T T -+
2075 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002076 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002077 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2078 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2079 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2080 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2081 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002082 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002083 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2084 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2085 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2086 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2087 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002088 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002089
Chris Lattner25eea4d2010-07-12 01:19:22 +00002090 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002091 // merging. However, bitcast of A->B->A are allowed.
2092 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2093 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2094 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2095
2096 // Check if any of the bitcasts convert scalars<->vectors.
2097 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2098 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2099 // Unless we are bitcasing to the original type, disallow optimizations.
2100 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002101
2102 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2103 [secondOp-Instruction::CastOpsBegin];
2104 switch (ElimCase) {
2105 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002106 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002107 return 0;
2108 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002109 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110 return firstOp;
2111 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002112 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 return secondOp;
2114 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002115 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002116 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002117 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002118 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002119 return firstOp;
2120 return 0;
2121 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002122 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002123 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002124 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002125 return firstOp;
2126 return 0;
2127 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002128 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002129 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002130 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002131 return secondOp;
2132 return 0;
2133 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002134 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002135 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002136 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002137 return secondOp;
2138 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002139 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002140 // Cannot simplify if address spaces are different!
2141 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2142 return 0;
2143
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002144 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002145 // We can still fold this without knowing the actual sizes as long we
2146 // know that the intermediate pointer is the largest possible
2147 // pointer size.
2148 // FIXME: Is this always true?
2149 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002150 return Instruction::BitCast;
2151
2152 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002153 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002154 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002155 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002156 if (MidSize >= PtrSize)
2157 return Instruction::BitCast;
2158 return 0;
2159 }
2160 case 8: {
2161 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2162 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2163 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002164 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2165 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166 if (SrcSize == DstSize)
2167 return Instruction::BitCast;
2168 else if (SrcSize < DstSize)
2169 return firstOp;
2170 return secondOp;
2171 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002172 case 9:
2173 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002174 return Instruction::ZExt;
2175 case 10:
2176 // fpext followed by ftrunc is allowed if the bit size returned to is
2177 // the same as the original, in which case its just a bitcast
2178 if (SrcTy == DstTy)
2179 return Instruction::BitCast;
2180 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002181 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002182 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002183 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002184 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002185 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002186 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2187 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002188 if (SrcSize <= PtrSize && SrcSize == DstSize)
2189 return Instruction::BitCast;
2190 return 0;
2191 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002192 case 12: {
2193 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2194 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2195 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2196 return Instruction::AddrSpaceCast;
2197 return Instruction::BitCast;
2198 }
2199 case 13:
2200 // FIXME: this state can be merged with (1), but the following assert
2201 // is useful to check the correcteness of the sequence due to semantic
2202 // change of bitcast.
2203 assert(
2204 SrcTy->isPtrOrPtrVectorTy() &&
2205 MidTy->isPtrOrPtrVectorTy() &&
2206 DstTy->isPtrOrPtrVectorTy() &&
2207 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2208 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2209 "Illegal addrspacecast, bitcast sequence!");
2210 // Allowed, use first cast's opcode
2211 return firstOp;
2212 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002213 // bitcast, addrspacecast -> addrspacecast if the element type of
2214 // bitcast's source is the same as that of addrspacecast's destination.
2215 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2216 return Instruction::AddrSpaceCast;
2217 return 0;
2218
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002219 case 15:
2220 // FIXME: this state can be merged with (1), but the following assert
2221 // is useful to check the correcteness of the sequence due to semantic
2222 // change of bitcast.
2223 assert(
2224 SrcTy->isIntOrIntVectorTy() &&
2225 MidTy->isPtrOrPtrVectorTy() &&
2226 DstTy->isPtrOrPtrVectorTy() &&
2227 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2228 "Illegal inttoptr, bitcast sequence!");
2229 // Allowed, use first cast's opcode
2230 return firstOp;
2231 case 16:
2232 // FIXME: this state can be merged with (2), but the following assert
2233 // is useful to check the correcteness of the sequence due to semantic
2234 // change of bitcast.
2235 assert(
2236 SrcTy->isPtrOrPtrVectorTy() &&
2237 MidTy->isPtrOrPtrVectorTy() &&
2238 DstTy->isIntOrIntVectorTy() &&
2239 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2240 "Illegal bitcast, ptrtoint sequence!");
2241 // Allowed, use second cast's opcode
2242 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002243 case 17:
2244 // (sitofp (zext x)) -> (uitofp x)
2245 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002246 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002247 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002249 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250 default:
Craig Topperc514b542012-02-05 22:14:15 +00002251 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002253}
2254
Chris Lattner229907c2011-07-18 04:54:35 +00002255CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002256 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002257 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002258 // Construct and return the appropriate CastInst subclass
2259 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002260 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2261 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2262 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2263 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2264 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2265 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2266 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2267 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2268 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2269 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2270 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2271 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2272 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2273 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002274 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275}
2276
Chris Lattner229907c2011-07-18 04:54:35 +00002277CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002278 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002279 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002280 // Construct and return the appropriate CastInst subclass
2281 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002282 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2283 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2284 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2285 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2286 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2287 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2288 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2289 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2290 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2291 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2292 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2293 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2294 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2295 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002297}
2298
Chris Lattner229907c2011-07-18 04:54:35 +00002299CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002300 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002301 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002302 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002303 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2304 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002305}
2306
Chris Lattner229907c2011-07-18 04:54:35 +00002307CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002308 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002309 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002310 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002311 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2312 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002313}
2314
Chris Lattner229907c2011-07-18 04:54:35 +00002315CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002316 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002317 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002318 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002319 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2320 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002321}
2322
Chris Lattner229907c2011-07-18 04:54:35 +00002323CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002324 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002325 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002326 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002327 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2328 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002329}
2330
Chris Lattner229907c2011-07-18 04:54:35 +00002331CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002332 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002333 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002334 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002335 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2336 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002337}
2338
Chris Lattner229907c2011-07-18 04:54:35 +00002339CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002340 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002341 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002342 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002343 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2344 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002345}
2346
Chris Lattner229907c2011-07-18 04:54:35 +00002347CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002348 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002349 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002350 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2351 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2352 "Invalid cast");
2353 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002354 assert((!Ty->isVectorTy() ||
2355 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002356 "Invalid cast");
2357
Matt Arsenault065ced92013-07-31 00:17:33 +00002358 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002359 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002360
Matt Arsenault740980e2014-07-14 17:24:35 +00002361 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002362}
2363
2364/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002365CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2366 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002367 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002368 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2369 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002370 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002371 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002372 assert((!Ty->isVectorTy() ||
2373 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002374 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002375
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002376 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002377 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002378
Matt Arsenault740980e2014-07-14 17:24:35 +00002379 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2380}
2381
2382CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2383 Value *S, Type *Ty,
2384 const Twine &Name,
2385 BasicBlock *InsertAtEnd) {
2386 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2387 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2388
2389 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2390 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2391
2392 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2393}
2394
2395CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2396 Value *S, Type *Ty,
2397 const Twine &Name,
2398 Instruction *InsertBefore) {
2399 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2400 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2401
2402 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002403 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2404
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002405 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002406}
2407
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002408CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2409 const Twine &Name,
2410 Instruction *InsertBefore) {
2411 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2412 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2413 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2414 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2415
2416 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2417}
2418
Matt Arsenault740980e2014-07-14 17:24:35 +00002419CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002420 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002421 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002422 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002423 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002424 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2425 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002426 Instruction::CastOps opcode =
2427 (SrcBits == DstBits ? Instruction::BitCast :
2428 (SrcBits > DstBits ? Instruction::Trunc :
2429 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002430 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002431}
2432
Chris Lattner229907c2011-07-18 04:54:35 +00002433CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002434 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002435 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002436 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002437 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002438 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2439 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002440 Instruction::CastOps opcode =
2441 (SrcBits == DstBits ? Instruction::BitCast :
2442 (SrcBits > DstBits ? Instruction::Trunc :
2443 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002444 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002445}
2446
Chris Lattner229907c2011-07-18 04:54:35 +00002447CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002448 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002449 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002450 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002451 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002452 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2453 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002454 Instruction::CastOps opcode =
2455 (SrcBits == DstBits ? Instruction::BitCast :
2456 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002457 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002458}
2459
Chris Lattner229907c2011-07-18 04:54:35 +00002460CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002461 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002462 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002463 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002464 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002465 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2466 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002467 Instruction::CastOps opcode =
2468 (SrcBits == DstBits ? Instruction::BitCast :
2469 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002470 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002471}
2472
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002473// Check whether it is valid to call getCastOpcode for these types.
2474// This routine must be kept in sync with getCastOpcode.
2475bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2476 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2477 return false;
2478
2479 if (SrcTy == DestTy)
2480 return true;
2481
2482 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2483 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2484 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2485 // An element by element cast. Valid if casting the elements is valid.
2486 SrcTy = SrcVecTy->getElementType();
2487 DestTy = DestVecTy->getElementType();
2488 }
2489
2490 // Get the bit sizes, we'll need these
2491 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2492 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2493
2494 // Run through the possibilities ...
2495 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002496 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002497 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002498 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002499 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002500 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002501 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002502 // Casting from something else
2503 return SrcTy->isPointerTy();
2504 }
2505 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2506 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002507 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002508 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002509 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002510 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002511 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002512 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002513 return false;
2514 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002515 if (DestTy->isVectorTy()) // Casting to vector
2516 return DestBits == SrcBits;
2517 if (DestTy->isPointerTy()) { // Casting to pointer
2518 if (SrcTy->isPointerTy()) // Casting from pointer
2519 return true;
2520 return SrcTy->isIntegerTy(); // Casting from integral
2521 }
2522 if (DestTy->isX86_MMXTy()) {
2523 if (SrcTy->isVectorTy())
2524 return DestBits == SrcBits; // 64-bit vector to MMX
2525 return false;
2526 } // Casting to something else
2527 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002528}
2529
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002530bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2531 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2532 return false;
2533
2534 if (SrcTy == DestTy)
2535 return true;
2536
2537 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2538 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2539 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2540 // An element by element cast. Valid if casting the elements is valid.
2541 SrcTy = SrcVecTy->getElementType();
2542 DestTy = DestVecTy->getElementType();
2543 }
2544 }
2545 }
2546
2547 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2548 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2549 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2550 }
2551 }
2552
2553 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2554 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2555
2556 // Could still have vectors of pointers if the number of elements doesn't
2557 // match
2558 if (SrcBits == 0 || DestBits == 0)
2559 return false;
2560
2561 if (SrcBits != DestBits)
2562 return false;
2563
2564 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2565 return false;
2566
2567 return true;
2568}
2569
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002570bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002571 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002572 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2573 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002574 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002575 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2576 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002577 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002578
2579 return isBitCastable(SrcTy, DestTy);
2580}
2581
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002582// Provide a way to get a "cast" where the cast opcode is inferred from the
2583// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002584// logic in the castIsValid function below. This axiom should hold:
2585// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2586// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002588// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002590CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002591 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2592 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593
Duncan Sands55e50902008-01-06 10:12:28 +00002594 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2595 "Only first class types are castable!");
2596
Duncan Sandsa8514532011-05-18 07:13:41 +00002597 if (SrcTy == DestTy)
2598 return BitCast;
2599
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002600 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002601 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2602 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002603 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2604 // An element by element cast. Find the appropriate opcode based on the
2605 // element types.
2606 SrcTy = SrcVecTy->getElementType();
2607 DestTy = DestVecTy->getElementType();
2608 }
2609
2610 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002611 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2612 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002613
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002615 if (DestTy->isIntegerTy()) { // Casting to integral
2616 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617 if (DestBits < SrcBits)
2618 return Trunc; // int -> smaller int
2619 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002620 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621 return SExt; // signed -> SEXT
2622 else
2623 return ZExt; // unsigned -> ZEXT
2624 } else {
2625 return BitCast; // Same size, No-op cast
2626 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002627 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002628 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002629 return FPToSI; // FP -> sint
2630 else
2631 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002632 } else if (SrcTy->isVectorTy()) {
2633 assert(DestBits == SrcBits &&
2634 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635 return BitCast; // Same size, no-op cast
2636 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002637 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002638 "Casting from a value that is not first-class type");
2639 return PtrToInt; // ptr -> int
2640 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002641 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2642 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002643 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002644 return SIToFP; // sint -> FP
2645 else
2646 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002647 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648 if (DestBits < SrcBits) {
2649 return FPTrunc; // FP -> smaller FP
2650 } else if (DestBits > SrcBits) {
2651 return FPExt; // FP -> larger FP
2652 } else {
2653 return BitCast; // same size, no-op cast
2654 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002655 } else if (SrcTy->isVectorTy()) {
2656 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002657 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002658 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002660 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002661 } else if (DestTy->isVectorTy()) {
2662 assert(DestBits == SrcBits &&
2663 "Illegal cast to vector (wrong type or size)");
2664 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002665 } else if (DestTy->isPointerTy()) {
2666 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002667 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2668 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002669 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002670 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002672 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002673 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002674 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002675 if (SrcTy->isVectorTy()) {
2676 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002677 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002678 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002679 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002680 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002681 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682}
2683
2684//===----------------------------------------------------------------------===//
2685// CastInst SubClass Constructors
2686//===----------------------------------------------------------------------===//
2687
2688/// Check that the construction parameters for a CastInst are correct. This
2689/// could be broken out into the separate constructors but it is useful to have
2690/// it in one place and to eliminate the redundant code for getting the sizes
2691/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002692bool
Chris Lattner229907c2011-07-18 04:54:35 +00002693CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002694
2695 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002696 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002697
Chris Lattner37bc78a2010-01-26 21:51:43 +00002698 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2699 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002700 return false;
2701
2702 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002703 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2704 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002705
Duncan Sands7f646562011-05-18 09:21:57 +00002706 // If these are vector types, get the lengths of the vectors (using zero for
2707 // scalar types means that checking that vector lengths match also checks that
2708 // scalars are not being converted to vectors or vectors to scalars).
2709 unsigned SrcLength = SrcTy->isVectorTy() ?
2710 cast<VectorType>(SrcTy)->getNumElements() : 0;
2711 unsigned DstLength = DstTy->isVectorTy() ?
2712 cast<VectorType>(DstTy)->getNumElements() : 0;
2713
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002714 // Switch on the opcode provided
2715 switch (op) {
2716 default: return false; // This is an input error
2717 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002718 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2719 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002720 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002721 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2722 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002723 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002724 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2725 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002726 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002727 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2728 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002729 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002730 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2731 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002732 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002733 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002734 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2735 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002736 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002737 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002738 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2739 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002740 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002741 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002742 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002743 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2744 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2745 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002746 return SrcTy->getScalarType()->isPointerTy() &&
2747 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002748 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002749 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002750 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002751 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2752 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2753 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002754 return SrcTy->getScalarType()->isIntegerTy() &&
2755 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002756 case Instruction::BitCast: {
2757 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2758 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2759
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002760 // BitCast implies a no-op cast of type only. No bits change.
2761 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002762 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002763 return false;
2764
Alp Tokerf907b892013-12-05 05:44:44 +00002765 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002766 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002767 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002768 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2769
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002770 // If both are pointers then the address spaces must match.
2771 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
2772 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002773
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002774 // A vector of pointers must have the same number of elements.
2775 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2776 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2777 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2778
2779 return false;
2780 }
2781
2782 return true;
2783 }
2784 case Instruction::AddrSpaceCast: {
2785 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2786 if (!SrcPtrTy)
2787 return false;
2788
2789 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2790 if (!DstPtrTy)
2791 return false;
2792
2793 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
2794 return false;
2795
2796 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2797 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2798 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2799
2800 return false;
2801 }
2802
2803 return true;
2804 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002805 }
2806}
2807
2808TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002809 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002810) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002811 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002812}
2813
2814TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002815 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002816) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002817 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002818}
2819
2820ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002821 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002822) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002823 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002824}
2825
2826ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002827 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002828) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002829 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002830}
2831SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002832 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002833) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002834 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002835}
2836
Jeff Cohencc08c832006-12-02 02:22:01 +00002837SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002838 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002839) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002840 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002841}
2842
2843FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002844 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002845) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002846 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002847}
2848
2849FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002850 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002851) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002852 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002853}
2854
2855FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002856 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002857) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002858 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002859}
2860
2861FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002862 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002863) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002864 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002865}
2866
2867UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002868 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002869) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002870 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002871}
2872
2873UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002874 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002875) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002876 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002877}
2878
2879SIToFPInst::SIToFPInst(
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, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002882 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002883}
2884
2885SIToFPInst::SIToFPInst(
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, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002888 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002889}
2890
2891FPToUIInst::FPToUIInst(
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, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002894 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002895}
2896
2897FPToUIInst::FPToUIInst(
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, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002900 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002901}
2902
2903FPToSIInst::FPToSIInst(
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, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002906 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002907}
2908
2909FPToSIInst::FPToSIInst(
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, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002912 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002913}
2914
2915PtrToIntInst::PtrToIntInst(
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, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002918 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002919}
2920
2921PtrToIntInst::PtrToIntInst(
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, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002924 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002925}
2926
2927IntToPtrInst::IntToPtrInst(
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, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002930 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002931}
2932
2933IntToPtrInst::IntToPtrInst(
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, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002936 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002937}
2938
2939BitCastInst::BitCastInst(
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, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002942 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002943}
2944
2945BitCastInst::BitCastInst(
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, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002948 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002950
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002951AddrSpaceCastInst::AddrSpaceCastInst(
2952 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2953) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
2954 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
2955}
2956
2957AddrSpaceCastInst::AddrSpaceCastInst(
2958 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2959) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
2960 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
2961}
2962
Chris Lattnerf16dc002006-09-17 19:29:56 +00002963//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002964// CmpInst Classes
2965//===----------------------------------------------------------------------===//
2966
Craig Topper3186c012012-09-23 02:12:10 +00002967void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002968
Chris Lattner229907c2011-07-18 04:54:35 +00002969CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002970 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002971 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002972 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002973 OperandTraits<CmpInst>::op_begin(this),
2974 OperandTraits<CmpInst>::operands(this),
2975 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002976 Op<0>() = LHS;
2977 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002978 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002979 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002980}
Gabor Greiff6caff662008-05-10 08:32:32 +00002981
Chris Lattner229907c2011-07-18 04:54:35 +00002982CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002983 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002984 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002985 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002986 OperandTraits<CmpInst>::op_begin(this),
2987 OperandTraits<CmpInst>::operands(this),
2988 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002989 Op<0>() = LHS;
2990 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002991 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002992 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002993}
2994
2995CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002996CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002997 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002998 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002999 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003000 if (InsertBefore)
3001 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3002 S1, S2, Name);
3003 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003004 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003005 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003006 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003007
3008 if (InsertBefore)
3009 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3010 S1, S2, Name);
3011 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003012 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003013 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003014}
3015
3016CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003017CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003018 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003019 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003020 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3021 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003022 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003023 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3024 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003025}
3026
3027void CmpInst::swapOperands() {
3028 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3029 IC->swapOperands();
3030 else
3031 cast<FCmpInst>(this)->swapOperands();
3032}
3033
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003034bool CmpInst::isCommutative() const {
3035 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003036 return IC->isCommutative();
3037 return cast<FCmpInst>(this)->isCommutative();
3038}
3039
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003040bool CmpInst::isEquality() const {
3041 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003042 return IC->isEquality();
3043 return cast<FCmpInst>(this)->isEquality();
3044}
3045
3046
Dan Gohman4e724382008-05-31 02:47:54 +00003047CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003048 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003049 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003050 case ICMP_EQ: return ICMP_NE;
3051 case ICMP_NE: return ICMP_EQ;
3052 case ICMP_UGT: return ICMP_ULE;
3053 case ICMP_ULT: return ICMP_UGE;
3054 case ICMP_UGE: return ICMP_ULT;
3055 case ICMP_ULE: return ICMP_UGT;
3056 case ICMP_SGT: return ICMP_SLE;
3057 case ICMP_SLT: return ICMP_SGE;
3058 case ICMP_SGE: return ICMP_SLT;
3059 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003060
Dan Gohman4e724382008-05-31 02:47:54 +00003061 case FCMP_OEQ: return FCMP_UNE;
3062 case FCMP_ONE: return FCMP_UEQ;
3063 case FCMP_OGT: return FCMP_ULE;
3064 case FCMP_OLT: return FCMP_UGE;
3065 case FCMP_OGE: return FCMP_ULT;
3066 case FCMP_OLE: return FCMP_UGT;
3067 case FCMP_UEQ: return FCMP_ONE;
3068 case FCMP_UNE: return FCMP_OEQ;
3069 case FCMP_UGT: return FCMP_OLE;
3070 case FCMP_ULT: return FCMP_OGE;
3071 case FCMP_UGE: return FCMP_OLT;
3072 case FCMP_ULE: return FCMP_OGT;
3073 case FCMP_ORD: return FCMP_UNO;
3074 case FCMP_UNO: return FCMP_ORD;
3075 case FCMP_TRUE: return FCMP_FALSE;
3076 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003077 }
3078}
3079
Reid Spencer266e42b2006-12-23 06:05:41 +00003080ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3081 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003082 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003083 case ICMP_EQ: case ICMP_NE:
3084 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3085 return pred;
3086 case ICMP_UGT: return ICMP_SGT;
3087 case ICMP_ULT: return ICMP_SLT;
3088 case ICMP_UGE: return ICMP_SGE;
3089 case ICMP_ULE: return ICMP_SLE;
3090 }
3091}
3092
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003093ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3094 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003095 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003096 case ICMP_EQ: case ICMP_NE:
3097 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3098 return pred;
3099 case ICMP_SGT: return ICMP_UGT;
3100 case ICMP_SLT: return ICMP_ULT;
3101 case ICMP_SGE: return ICMP_UGE;
3102 case ICMP_SLE: return ICMP_ULE;
3103 }
3104}
3105
Reid Spencer0286bc12007-02-28 22:00:54 +00003106/// Initialize a set of values that all satisfy the condition with C.
3107///
3108ConstantRange
3109ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3110 APInt Lower(C);
3111 APInt Upper(C);
3112 uint32_t BitWidth = C.getBitWidth();
3113 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003114 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003115 case ICmpInst::ICMP_EQ: ++Upper; break;
3116 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003117 case ICmpInst::ICMP_ULT:
3118 Lower = APInt::getMinValue(BitWidth);
3119 // Check for an empty-set condition.
3120 if (Lower == Upper)
3121 return ConstantRange(BitWidth, /*isFullSet=*/false);
3122 break;
3123 case ICmpInst::ICMP_SLT:
3124 Lower = APInt::getSignedMinValue(BitWidth);
3125 // Check for an empty-set condition.
3126 if (Lower == Upper)
3127 return ConstantRange(BitWidth, /*isFullSet=*/false);
3128 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003129 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003130 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003131 // Check for an empty-set condition.
3132 if (Lower == Upper)
3133 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003134 break;
3135 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003136 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003137 // Check for an empty-set condition.
3138 if (Lower == Upper)
3139 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003140 break;
3141 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003142 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003143 // Check for a full-set condition.
3144 if (Lower == Upper)
3145 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003146 break;
3147 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003148 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003149 // Check for a full-set condition.
3150 if (Lower == Upper)
3151 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003152 break;
3153 case ICmpInst::ICMP_UGE:
3154 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003155 // Check for a full-set condition.
3156 if (Lower == Upper)
3157 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003158 break;
3159 case ICmpInst::ICMP_SGE:
3160 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003161 // Check for a full-set condition.
3162 if (Lower == Upper)
3163 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003164 break;
3165 }
3166 return ConstantRange(Lower, Upper);
3167}
3168
Dan Gohman4e724382008-05-31 02:47:54 +00003169CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003170 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003171 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003172 case ICMP_EQ: case ICMP_NE:
3173 return pred;
3174 case ICMP_SGT: return ICMP_SLT;
3175 case ICMP_SLT: return ICMP_SGT;
3176 case ICMP_SGE: return ICMP_SLE;
3177 case ICMP_SLE: return ICMP_SGE;
3178 case ICMP_UGT: return ICMP_ULT;
3179 case ICMP_ULT: return ICMP_UGT;
3180 case ICMP_UGE: return ICMP_ULE;
3181 case ICMP_ULE: return ICMP_UGE;
3182
Reid Spencerd9436b62006-11-20 01:22:35 +00003183 case FCMP_FALSE: case FCMP_TRUE:
3184 case FCMP_OEQ: case FCMP_ONE:
3185 case FCMP_UEQ: case FCMP_UNE:
3186 case FCMP_ORD: case FCMP_UNO:
3187 return pred;
3188 case FCMP_OGT: return FCMP_OLT;
3189 case FCMP_OLT: return FCMP_OGT;
3190 case FCMP_OGE: return FCMP_OLE;
3191 case FCMP_OLE: return FCMP_OGE;
3192 case FCMP_UGT: return FCMP_ULT;
3193 case FCMP_ULT: return FCMP_UGT;
3194 case FCMP_UGE: return FCMP_ULE;
3195 case FCMP_ULE: return FCMP_UGE;
3196 }
3197}
3198
Reid Spencer266e42b2006-12-23 06:05:41 +00003199bool CmpInst::isUnsigned(unsigned short predicate) {
3200 switch (predicate) {
3201 default: return false;
3202 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3203 case ICmpInst::ICMP_UGE: return true;
3204 }
3205}
3206
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003207bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003208 switch (predicate) {
3209 default: return false;
3210 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3211 case ICmpInst::ICMP_SGE: return true;
3212 }
3213}
3214
3215bool CmpInst::isOrdered(unsigned short predicate) {
3216 switch (predicate) {
3217 default: return false;
3218 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3219 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3220 case FCmpInst::FCMP_ORD: return true;
3221 }
3222}
3223
3224bool CmpInst::isUnordered(unsigned short predicate) {
3225 switch (predicate) {
3226 default: return false;
3227 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3228 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3229 case FCmpInst::FCMP_UNO: return true;
3230 }
3231}
3232
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003233bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3234 switch(predicate) {
3235 default: return false;
3236 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3237 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3238 }
3239}
3240
3241bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3242 switch(predicate) {
3243 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3244 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3245 default: return false;
3246 }
3247}
3248
3249
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003250//===----------------------------------------------------------------------===//
3251// SwitchInst Implementation
3252//===----------------------------------------------------------------------===//
3253
Chris Lattnerbaf00152010-11-17 05:41:46 +00003254void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3255 assert(Value && Default && NumReserved);
3256 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003257 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003258 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003259
Pete Coopereb31b682015-05-21 22:48:54 +00003260 Op<0>() = Value;
3261 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003262}
3263
Chris Lattner2195fc42007-02-24 00:55:48 +00003264/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3265/// switch on and a default destination. The number of additional cases can
3266/// be specified here to make memory allocation more efficient. This
3267/// constructor can also autoinsert before another instruction.
3268SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3269 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003270 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003271 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003272 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003273}
3274
3275/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3276/// switch on and a default destination. The number of additional cases can
3277/// be specified here to make memory allocation more efficient. This
3278/// constructor also autoinserts at the end of the specified BasicBlock.
3279SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3280 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003281 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003282 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003283 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003284}
3285
Misha Brukmanb1c93172005-04-21 23:48:37 +00003286SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003287 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003288 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003289 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003290 Use *OL = getOperandList();
3291 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003292 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003293 OL[i] = InOL[i];
3294 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003295 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003296 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003297}
3298
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003299
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003300/// addCase - Add an entry to the switch instruction...
3301///
Chris Lattner47ac1872005-02-24 05:32:09 +00003302void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003303 unsigned NewCaseIdx = getNumCases();
3304 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003305 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003306 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003307 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003308 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003309 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003310 CaseIt Case(this, NewCaseIdx);
3311 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003312 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003313}
3314
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003315/// removeCase - This method removes the specified case and its successor
3316/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003317void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003318 unsigned idx = i.getCaseIndex();
3319
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003320 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003321
3322 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003323 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003324
Jay Foad14277722011-02-01 09:22:34 +00003325 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003326 if (2 + (idx + 1) * 2 != NumOps) {
3327 OL[2 + idx * 2] = OL[NumOps - 2];
3328 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003329 }
3330
3331 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003332 OL[NumOps-2].set(nullptr);
3333 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003334 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003335}
3336
Jay Foade98f29d2011-04-01 08:00:58 +00003337/// growOperands - grow operands - This grows the operand list in response
3338/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003339///
Jay Foade98f29d2011-04-01 08:00:58 +00003340void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003341 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003342 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003343
3344 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003345 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003346}
3347
3348
3349BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3350 return getSuccessor(idx);
3351}
3352unsigned SwitchInst::getNumSuccessorsV() const {
3353 return getNumSuccessors();
3354}
3355void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3356 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003357}
Chris Lattnerf22be932004-10-15 23:52:53 +00003358
Chris Lattner3ed871f2009-10-27 19:13:16 +00003359//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003360// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003361//===----------------------------------------------------------------------===//
3362
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003363void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003364 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003365 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003366 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003367 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003368 allocHungoffUses(ReservedSpace);
3369
Pete Coopereb31b682015-05-21 22:48:54 +00003370 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003371}
3372
3373
Jay Foade98f29d2011-04-01 08:00:58 +00003374/// growOperands - grow operands - This grows the operand list in response
3375/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003376///
Jay Foade98f29d2011-04-01 08:00:58 +00003377void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003378 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003379 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003380
3381 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003382 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003383}
3384
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003385IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3386 Instruction *InsertBefore)
3387: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003388 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003389 init(Address, NumCases);
3390}
3391
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003392IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3393 BasicBlock *InsertAtEnd)
3394: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003395 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003396 init(Address, NumCases);
3397}
3398
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003399IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003400 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3401 nullptr, IBI.getNumOperands()) {
3402 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003403 Use *OL = getOperandList();
3404 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003405 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3406 OL[i] = InOL[i];
3407 SubclassOptionalData = IBI.SubclassOptionalData;
3408}
3409
Chris Lattner3ed871f2009-10-27 19:13:16 +00003410/// addDestination - Add a destination.
3411///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003412void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003413 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003414 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003415 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003416 // Initialize some new operands.
3417 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003418 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003419 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003420}
3421
3422/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003423/// indirectbr instruction.
3424void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003425 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3426
3427 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003428 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003429
3430 // Replace this value with the last one.
3431 OL[idx+1] = OL[NumOps-1];
3432
3433 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003434 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003435 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003436}
3437
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003438BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003439 return getSuccessor(idx);
3440}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003441unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003442 return getNumSuccessors();
3443}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003444void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003445 setSuccessor(idx, B);
3446}
3447
3448//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003449// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003450//===----------------------------------------------------------------------===//
3451
Chris Lattnerf22be932004-10-15 23:52:53 +00003452// Define these methods here so vtables don't get emitted into every translation
3453// unit that uses these classes.
3454
Pete Cooper75403d72015-06-24 20:22:23 +00003455GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003456 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003457}
3458
Pete Cooper75403d72015-06-24 20:22:23 +00003459BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003460 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003461}
3462
Pete Cooper75403d72015-06-24 20:22:23 +00003463FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003464 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003465}
3466
Pete Cooper75403d72015-06-24 20:22:23 +00003467ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003468 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003469}
3470
Pete Cooper75403d72015-06-24 20:22:23 +00003471ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003472 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003473}
3474
Pete Cooper75403d72015-06-24 20:22:23 +00003475InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003476 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003477}
3478
Pete Cooper75403d72015-06-24 20:22:23 +00003479AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003480 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3481 (Value *)getOperand(0), getAlignment());
3482 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3483 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003484}
3485
Pete Cooper75403d72015-06-24 20:22:23 +00003486LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003487 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3488 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003489}
3490
Pete Cooper75403d72015-06-24 20:22:23 +00003491StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003492 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003493 getAlignment(), getOrdering(), getSynchScope());
3494
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003495}
3496
Pete Cooper75403d72015-06-24 20:22:23 +00003497AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003498 AtomicCmpXchgInst *Result =
3499 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003500 getSuccessOrdering(), getFailureOrdering(),
3501 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003502 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003503 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003504 return Result;
3505}
3506
Pete Cooper75403d72015-06-24 20:22:23 +00003507AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003508 AtomicRMWInst *Result =
3509 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3510 getOrdering(), getSynchScope());
3511 Result->setVolatile(isVolatile());
3512 return Result;
3513}
3514
Pete Cooper75403d72015-06-24 20:22:23 +00003515FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003516 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3517}
3518
Pete Cooper75403d72015-06-24 20:22:23 +00003519TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003520 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003521}
3522
Pete Cooper75403d72015-06-24 20:22:23 +00003523ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003524 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003525}
3526
Pete Cooper75403d72015-06-24 20:22:23 +00003527SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003528 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003529}
3530
Pete Cooper75403d72015-06-24 20:22:23 +00003531FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003532 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003533}
3534
Pete Cooper75403d72015-06-24 20:22:23 +00003535FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003536 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003537}
3538
Pete Cooper75403d72015-06-24 20:22:23 +00003539UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003540 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003541}
3542
Pete Cooper75403d72015-06-24 20:22:23 +00003543SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003544 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003545}
3546
Pete Cooper75403d72015-06-24 20:22:23 +00003547FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003548 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003549}
3550
Pete Cooper75403d72015-06-24 20:22:23 +00003551FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003552 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003553}
3554
Pete Cooper75403d72015-06-24 20:22:23 +00003555PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003556 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003557}
3558
Pete Cooper75403d72015-06-24 20:22:23 +00003559IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003560 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003561}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003562
Pete Cooper75403d72015-06-24 20:22:23 +00003563BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003564 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003565}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003566
Pete Cooper75403d72015-06-24 20:22:23 +00003567AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003568 return new AddrSpaceCastInst(getOperand(0), getType());
3569}
3570
Pete Cooper75403d72015-06-24 20:22:23 +00003571CallInst *CallInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003572 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003573}
3574
Pete Cooper75403d72015-06-24 20:22:23 +00003575SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003576 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003577}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003578
Pete Cooper75403d72015-06-24 20:22:23 +00003579VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003580 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003581}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003582
Pete Cooper75403d72015-06-24 20:22:23 +00003583ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003584 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003585}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003586
Pete Cooper75403d72015-06-24 20:22:23 +00003587InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003588 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003589}
3590
Pete Cooper75403d72015-06-24 20:22:23 +00003591ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003592 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003593}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003594
Pete Cooper75403d72015-06-24 20:22:23 +00003595PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003596
Pete Cooper75403d72015-06-24 20:22:23 +00003597LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003598 return new LandingPadInst(*this);
3599}
3600
Pete Cooper75403d72015-06-24 20:22:23 +00003601ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003602 return new(getNumOperands()) ReturnInst(*this);
3603}
3604
Pete Cooper75403d72015-06-24 20:22:23 +00003605BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003606 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003607}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003608
Pete Cooper75403d72015-06-24 20:22:23 +00003609SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003610
Pete Cooper75403d72015-06-24 20:22:23 +00003611IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003612 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003613}
3614
Pete Cooper75403d72015-06-24 20:22:23 +00003615InvokeInst *InvokeInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003616 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003617}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003618
Pete Cooper75403d72015-06-24 20:22:23 +00003619ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003620
Pete Cooper75403d72015-06-24 20:22:23 +00003621UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003622 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003623 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003624}