blob: d45b5110536110dea4abcbaf13203198512a0f6f [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
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000295void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000296 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000297 AttrBuilder B(attr);
298 LLVMContext &Context = getContext();
299 PAL = PAL.removeAttributes(Context, i,
300 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000301 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000302}
303
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000304void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
305 AttributeSet PAL = getAttributes();
306 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
307 setAttributes(PAL);
308}
309
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000310void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
311 AttributeSet PAL = getAttributes();
312 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
313 setAttributes(PAL);
314}
315
Michael Gottesman41748d72013-06-27 00:25:01 +0000316bool CallInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000317 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000318 return true;
319 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000320 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000321 return false;
322}
323
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000324bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000325 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000326 return true;
327 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000328 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000329 return false;
330}
331
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000332/// IsConstantOne - Return true only if val is constant int 1
333static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000334 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000335 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
336 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000337}
338
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000339static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000340 BasicBlock *InsertAtEnd, Type *IntPtrTy,
341 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000342 Value *ArraySize, Function *MallocF,
343 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000344 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000345 "createMalloc needs either InsertBefore or InsertAtEnd");
346
347 // malloc(type) becomes:
348 // bitcast (i8* malloc(typeSize)) to type*
349 // malloc(type, arraySize) becomes:
350 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000351 if (!ArraySize)
352 ArraySize = ConstantInt::get(IntPtrTy, 1);
353 else if (ArraySize->getType() != IntPtrTy) {
354 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000355 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
356 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000357 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000358 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
359 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000360 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000361
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000362 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000363 if (IsConstantOne(AllocSize)) {
364 AllocSize = ArraySize; // Operand * 1 = Operand
365 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
366 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
367 false /*ZExt*/);
368 // Malloc arg is constant product of type size and array size
369 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
370 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000371 // Multiply type size by the array size...
372 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000373 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
374 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000375 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000376 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
377 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000378 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000379 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000380
Victor Hernandez788eaab2009-09-18 19:20:02 +0000381 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000382 // Create the call to Malloc.
383 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
384 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000385 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000386 Value *MallocFunc = MallocF;
387 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000388 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000389 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000390 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000391 CallInst *MCall = nullptr;
392 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000393 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000394 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000395 Result = MCall;
396 if (Result->getType() != AllocPtrType)
397 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000398 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000399 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000400 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000401 Result = MCall;
402 if (Result->getType() != AllocPtrType) {
403 InsertAtEnd->getInstList().push_back(MCall);
404 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000405 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000406 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000407 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000409 if (Function *F = dyn_cast<Function>(MallocFunc)) {
410 MCall->setCallingConv(F->getCallingConv());
411 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
412 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000413 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000414
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000415 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000416}
417
418/// CreateMalloc - Generate the IR for a call to malloc:
419/// 1. Compute the malloc call's argument as the specified type's size,
420/// possibly multiplied by the array size if the array size is not
421/// constant 1.
422/// 2. Call malloc with that argument.
423/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000424Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000425 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000426 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000427 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000428 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000429 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000430 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000431}
432
433/// CreateMalloc - Generate the IR for a call to malloc:
434/// 1. Compute the malloc call's argument as the specified type's size,
435/// possibly multiplied by the array size if the array size is not
436/// constant 1.
437/// 2. Call malloc with that argument.
438/// 3. Bitcast the result of the malloc call to the specified type.
439/// Note: This function does not add the bitcast to the basic block, that is the
440/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000441Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000442 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000443 Value *AllocSize, Value *ArraySize,
444 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000445 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000446 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000447}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000448
Victor Hernandeze2971492009-10-24 04:23:03 +0000449static Instruction* createFree(Value* Source, Instruction *InsertBefore,
450 BasicBlock *InsertAtEnd) {
451 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
452 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000453 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000454 "Can not free something of nonpointer type!");
455
456 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
457 Module* M = BB->getParent()->getParent();
458
Chris Lattner229907c2011-07-18 04:54:35 +0000459 Type *VoidTy = Type::getVoidTy(M->getContext());
460 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000461 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000462 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000463 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000464 Value *PtrCast = Source;
465 if (InsertBefore) {
466 if (Source->getType() != IntPtrTy)
467 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
468 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
469 } else {
470 if (Source->getType() != IntPtrTy)
471 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
472 Result = CallInst::Create(FreeFunc, PtrCast, "");
473 }
474 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000475 if (Function *F = dyn_cast<Function>(FreeFunc))
476 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000477
478 return Result;
479}
480
481/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000482Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000483 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000484}
485
486/// CreateFree - Generate the IR for a call to the builtin free function.
487/// Note: This function does not add the call to the basic block, that is the
488/// responsibility of the caller.
489Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000490 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000491 assert(FreeCall && "CreateFree did not create a CallInst");
492 return FreeCall;
493}
494
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000495//===----------------------------------------------------------------------===//
496// InvokeInst Implementation
497//===----------------------------------------------------------------------===//
498
David Blaikie3e807092015-05-13 18:35:26 +0000499void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
500 BasicBlock *IfException, ArrayRef<Value *> Args,
501 const Twine &NameStr) {
502 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000503
Pete Cooperb4eede22015-06-12 17:48:10 +0000504 assert(getNumOperands() == 3 + Args.size() && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000505 Op<-3>() = Fn;
506 Op<-2>() = IfNormal;
507 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000508
509#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000510 assert(((Args.size() == FTy->getNumParams()) ||
511 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000512 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000513
Jay Foad5bd375a2011-07-15 08:37:34 +0000514 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000515 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000516 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000517 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000518#endif
519
520 std::copy(Args.begin(), Args.end(), op_begin());
521 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000522}
523
Misha Brukmanb1c93172005-04-21 23:48:37 +0000524InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000525 : TerminatorInst(II.getType(), Instruction::Invoke,
526 OperandTraits<InvokeInst>::op_end(this) -
527 II.getNumOperands(),
528 II.getNumOperands()),
529 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000530 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000531 std::copy(II.op_begin(), II.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000532 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000533}
534
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000535BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
536 return getSuccessor(idx);
537}
538unsigned InvokeInst::getNumSuccessorsV() const {
539 return getNumSuccessors();
540}
541void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
542 return setSuccessor(idx, B);
543}
544
Michael Gottesman41748d72013-06-27 00:25:01 +0000545bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000546 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000547 return true;
548 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000549 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000550 return false;
551}
552
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000553bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000554 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000555 return true;
556 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000557 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000558 return false;
559}
560
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000561void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000562 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000563 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000564 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000565}
566
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000567void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000568 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000569 AttrBuilder B(attr);
570 PAL = PAL.removeAttributes(getContext(), i,
571 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000572 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000573}
574
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000575void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
576 AttributeSet PAL = getAttributes();
577 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
578 setAttributes(PAL);
579}
580
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000581void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
582 AttributeSet PAL = getAttributes();
583 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
584 setAttributes(PAL);
585}
586
Bill Wendlingfae14752011-08-12 20:24:12 +0000587LandingPadInst *InvokeInst::getLandingPadInst() const {
588 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
589}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000590
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000591//===----------------------------------------------------------------------===//
592// ReturnInst Implementation
593//===----------------------------------------------------------------------===//
594
Chris Lattner2195fc42007-02-24 00:55:48 +0000595ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000596 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000597 OperandTraits<ReturnInst>::op_end(this) -
598 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000599 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000600 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000601 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000602 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000603}
604
Owen Anderson55f1c092009-08-13 21:58:54 +0000605ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
606 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000607 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
608 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000609 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000610 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000611}
Owen Anderson55f1c092009-08-13 21:58:54 +0000612ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
613 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000614 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
615 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000616 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000617 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000618}
Owen Anderson55f1c092009-08-13 21:58:54 +0000619ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
620 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000621 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000622}
623
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624unsigned ReturnInst::getNumSuccessorsV() const {
625 return getNumSuccessors();
626}
627
Devang Patelae682fb2008-02-26 17:56:20 +0000628/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
629/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000630void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000631 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000632}
633
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000634BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000635 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636}
637
Devang Patel59643e52008-02-23 00:35:18 +0000638ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000639}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000640
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000641//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000642// ResumeInst Implementation
643//===----------------------------------------------------------------------===//
644
645ResumeInst::ResumeInst(const ResumeInst &RI)
646 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
647 OperandTraits<ResumeInst>::op_begin(this), 1) {
648 Op<0>() = RI.Op<0>();
649}
650
651ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
652 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
653 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
654 Op<0>() = Exn;
655}
656
657ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
658 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
659 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
660 Op<0>() = Exn;
661}
662
663unsigned ResumeInst::getNumSuccessorsV() const {
664 return getNumSuccessors();
665}
666
667void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
668 llvm_unreachable("ResumeInst has no successors!");
669}
670
671BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
672 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000673}
674
675//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000676// UnreachableInst Implementation
677//===----------------------------------------------------------------------===//
678
Owen Anderson55f1c092009-08-13 21:58:54 +0000679UnreachableInst::UnreachableInst(LLVMContext &Context,
680 Instruction *InsertBefore)
681 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000682 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000683}
Owen Anderson55f1c092009-08-13 21:58:54 +0000684UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
685 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000686 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000687}
688
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000689unsigned UnreachableInst::getNumSuccessorsV() const {
690 return getNumSuccessors();
691}
692
693void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000694 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000695}
696
697BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +0000698 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000699}
700
701//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000702// BranchInst Implementation
703//===----------------------------------------------------------------------===//
704
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705void BranchInst::AssertOK() {
706 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000707 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000708 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000709}
710
Chris Lattner2195fc42007-02-24 00:55:48 +0000711BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000712 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000713 OperandTraits<BranchInst>::op_end(this) - 1,
714 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000715 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000716 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000717}
718BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
719 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000720 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000721 OperandTraits<BranchInst>::op_end(this) - 3,
722 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000723 Op<-1>() = IfTrue;
724 Op<-2>() = IfFalse;
725 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000726#ifndef NDEBUG
727 AssertOK();
728#endif
729}
730
731BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000732 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000733 OperandTraits<BranchInst>::op_end(this) - 1,
734 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000735 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000736 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000737}
738
739BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
740 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000741 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000742 OperandTraits<BranchInst>::op_end(this) - 3,
743 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000744 Op<-1>() = IfTrue;
745 Op<-2>() = IfFalse;
746 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000747#ifndef NDEBUG
748 AssertOK();
749#endif
750}
751
752
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000753BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000754 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000755 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
756 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000757 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000758 if (BI.getNumOperands() != 1) {
759 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000760 Op<-3>() = BI.Op<-3>();
761 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000762 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000763 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000764}
765
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000766void BranchInst::swapSuccessors() {
767 assert(isConditional() &&
768 "Cannot swap successors of an unconditional branch");
769 Op<-1>().swap(Op<-2>());
770
771 // Update profile metadata if present and it matches our structural
772 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000773 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000774 if (!ProfileData || ProfileData->getNumOperands() != 3)
775 return;
776
777 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000778 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
779 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000780 setMetadata(LLVMContext::MD_prof,
781 MDNode::get(ProfileData->getContext(), Ops));
782}
783
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
785 return getSuccessor(idx);
786}
787unsigned BranchInst::getNumSuccessorsV() const {
788 return getNumSuccessors();
789}
790void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
791 setSuccessor(idx, B);
792}
793
794
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000795//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000796// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797//===----------------------------------------------------------------------===//
798
Owen Andersonb6b25302009-07-14 23:09:55 +0000799static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000800 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000801 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000802 else {
803 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000804 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000805 assert(Amt->getType()->isIntegerTy() &&
806 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000807 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000808 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000809}
810
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000811AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
812 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000813
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000814AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
815 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000816
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000817AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000818 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000819 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000820
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000821AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000822 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000823 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000824
Chris Lattner229907c2011-07-18 04:54:35 +0000825AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000826 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +0000827 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
828 getAISize(Ty->getContext(), ArraySize), InsertBefore),
829 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000830 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000831 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000832 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000833}
834
Chris Lattner229907c2011-07-18 04:54:35 +0000835AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000836 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +0000837 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
838 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
839 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000840 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000841 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000842 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000843}
844
Gordon Henriksen14a55692007-12-10 02:14:30 +0000845// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000846AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000847}
848
Victor Hernandez8acf2952009-10-23 21:09:37 +0000849void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000850 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000851 assert(Align <= MaximumAlignment &&
852 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +0000853 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
854 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +0000855 assert(getAlignment() == Align && "Alignment representation error!");
856}
857
Victor Hernandez8acf2952009-10-23 21:09:37 +0000858bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000859 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000860 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000861 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000862}
863
Chris Lattner8b291e62008-11-26 02:54:17 +0000864/// isStaticAlloca - Return true if this alloca is in the entry block of the
865/// function and is a constant size. If so, the code generator will fold it
866/// into the prolog/epilog code, so it is basically free.
867bool AllocaInst::isStaticAlloca() const {
868 // Must be constant size.
869 if (!isa<ConstantInt>(getArraySize())) return false;
870
871 // Must be in the entry block.
872 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +0000873 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +0000874}
875
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000876//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000877// LoadInst Implementation
878//===----------------------------------------------------------------------===//
879
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000880void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000881 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000882 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +0000883 assert(!(isAtomic() && getAlignment() == 0) &&
884 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000885}
886
Daniel Dunbar4975db62009-07-25 04:41:11 +0000887LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000888 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889
Daniel Dunbar4975db62009-07-25 04:41:11 +0000890LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000891 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000892
David Blaikie0c28fd72015-05-20 21:46:30 +0000893LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000894 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +0000895 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +0000896
897LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
898 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000899 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +0000900
David Blaikieb7a029872015-04-17 19:56:21 +0000901LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000902 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +0000903 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000904 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000905
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000906LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000907 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +0000908 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +0000909}
910
David Blaikie15d9a4c2015-04-06 20:59:48 +0000911LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +0000912 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +0000913 SynchronizationScope SynchScope, Instruction *InsertBef)
914 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +0000915 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +0000916 setVolatile(isVolatile);
917 setAlignment(Align);
918 setAtomic(Order, SynchScope);
919 AssertOK();
920 setName(Name);
921}
922
923LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
924 unsigned Align, AtomicOrdering Order,
925 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000926 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000927 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000928 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000929 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +0000930 setAlignment(Align);
931 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +0000932 AssertOK();
933 setName(Name);
934}
935
Daniel Dunbar27096822009-08-11 18:11:15 +0000936LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
937 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
938 Load, Ptr, InsertBef) {
939 setVolatile(false);
940 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000941 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000942 AssertOK();
943 if (Name && Name[0]) setName(Name);
944}
945
946LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
947 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
948 Load, Ptr, InsertAE) {
949 setVolatile(false);
950 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000951 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000952 AssertOK();
953 if (Name && Name[0]) setName(Name);
954}
955
David Blaikie0c28fd72015-05-20 21:46:30 +0000956LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +0000957 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +0000958 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
959 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +0000960 setVolatile(isVolatile);
961 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000962 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000963 AssertOK();
964 if (Name && Name[0]) setName(Name);
965}
966
967LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
968 BasicBlock *InsertAE)
969 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
970 Load, Ptr, InsertAE) {
971 setVolatile(isVolatile);
972 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +0000973 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +0000974 AssertOK();
975 if (Name && Name[0]) setName(Name);
976}
977
Christopher Lamb84485702007-04-22 19:24:39 +0000978void LoadInst::setAlignment(unsigned Align) {
979 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000980 assert(Align <= MaximumAlignment &&
981 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +0000982 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000983 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000984 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000985}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000986
987//===----------------------------------------------------------------------===//
988// StoreInst Implementation
989//===----------------------------------------------------------------------===//
990
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000991void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000992 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +0000993 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000994 "Ptr must have pointer type!");
995 assert(getOperand(0)->getType() ==
996 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000997 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +0000998 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +0000999 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001000}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001001
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001002StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001003 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001004
1005StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001006 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001007
Misha Brukmanb1c93172005-04-21 23:48:37 +00001008StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001009 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001010 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001011
1012StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001013 BasicBlock *InsertAtEnd)
1014 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1015
1016StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1017 Instruction *InsertBefore)
1018 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1019 InsertBefore) {}
1020
1021StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1022 BasicBlock *InsertAtEnd)
1023 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1024 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001025
Misha Brukmanb1c93172005-04-21 23:48:37 +00001026StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001027 unsigned Align, AtomicOrdering Order,
1028 SynchronizationScope SynchScope,
1029 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001030 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001031 OperandTraits<StoreInst>::op_begin(this),
1032 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001033 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001034 Op<0>() = val;
1035 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001036 setVolatile(isVolatile);
1037 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001038 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001039 AssertOK();
1040}
1041
1042StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001043 unsigned Align, AtomicOrdering Order,
1044 SynchronizationScope SynchScope,
1045 BasicBlock *InsertAtEnd)
1046 : Instruction(Type::getVoidTy(val->getContext()), Store,
1047 OperandTraits<StoreInst>::op_begin(this),
1048 OperandTraits<StoreInst>::operands(this),
1049 InsertAtEnd) {
1050 Op<0>() = val;
1051 Op<1>() = addr;
1052 setVolatile(isVolatile);
1053 setAlignment(Align);
1054 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001055 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001056}
1057
Christopher Lamb84485702007-04-22 19:24:39 +00001058void StoreInst::setAlignment(unsigned Align) {
1059 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001060 assert(Align <= MaximumAlignment &&
1061 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001062 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001063 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001064 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001065}
1066
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001067//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001068// AtomicCmpXchgInst Implementation
1069//===----------------------------------------------------------------------===//
1070
1071void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001072 AtomicOrdering SuccessOrdering,
1073 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001074 SynchronizationScope SynchScope) {
1075 Op<0>() = Ptr;
1076 Op<1>() = Cmp;
1077 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001078 setSuccessOrdering(SuccessOrdering);
1079 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001080 setSynchScope(SynchScope);
1081
1082 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1083 "All operands must be non-null!");
1084 assert(getOperand(0)->getType()->isPointerTy() &&
1085 "Ptr must have pointer type!");
1086 assert(getOperand(1)->getType() ==
1087 cast<PointerType>(getOperand(0)->getType())->getElementType()
1088 && "Ptr must be a pointer to Cmp type!");
1089 assert(getOperand(2)->getType() ==
1090 cast<PointerType>(getOperand(0)->getType())->getElementType()
1091 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001092 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001093 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001094 assert(FailureOrdering != NotAtomic &&
1095 "AtomicCmpXchg instructions must be atomic!");
1096 assert(SuccessOrdering >= FailureOrdering &&
1097 "AtomicCmpXchg success ordering must be at least as strong as fail");
1098 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1099 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001100}
1101
1102AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001103 AtomicOrdering SuccessOrdering,
1104 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001105 SynchronizationScope SynchScope,
1106 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001107 : Instruction(
1108 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1109 nullptr),
1110 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1111 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001112 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001113}
1114
1115AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001116 AtomicOrdering SuccessOrdering,
1117 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001118 SynchronizationScope SynchScope,
1119 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001120 : Instruction(
1121 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1122 nullptr),
1123 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1124 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001125 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001126}
Tim Northover420a2162014-06-13 14:24:07 +00001127
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001128//===----------------------------------------------------------------------===//
1129// AtomicRMWInst Implementation
1130//===----------------------------------------------------------------------===//
1131
1132void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1133 AtomicOrdering Ordering,
1134 SynchronizationScope SynchScope) {
1135 Op<0>() = Ptr;
1136 Op<1>() = Val;
1137 setOperation(Operation);
1138 setOrdering(Ordering);
1139 setSynchScope(SynchScope);
1140
1141 assert(getOperand(0) && getOperand(1) &&
1142 "All operands must be non-null!");
1143 assert(getOperand(0)->getType()->isPointerTy() &&
1144 "Ptr must have pointer type!");
1145 assert(getOperand(1)->getType() ==
1146 cast<PointerType>(getOperand(0)->getType())->getElementType()
1147 && "Ptr must be a pointer to Val type!");
1148 assert(Ordering != NotAtomic &&
1149 "AtomicRMW instructions must be atomic!");
1150}
1151
1152AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1153 AtomicOrdering Ordering,
1154 SynchronizationScope SynchScope,
1155 Instruction *InsertBefore)
1156 : Instruction(Val->getType(), AtomicRMW,
1157 OperandTraits<AtomicRMWInst>::op_begin(this),
1158 OperandTraits<AtomicRMWInst>::operands(this),
1159 InsertBefore) {
1160 Init(Operation, Ptr, Val, Ordering, SynchScope);
1161}
1162
1163AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1164 AtomicOrdering Ordering,
1165 SynchronizationScope SynchScope,
1166 BasicBlock *InsertAtEnd)
1167 : Instruction(Val->getType(), AtomicRMW,
1168 OperandTraits<AtomicRMWInst>::op_begin(this),
1169 OperandTraits<AtomicRMWInst>::operands(this),
1170 InsertAtEnd) {
1171 Init(Operation, Ptr, Val, Ordering, SynchScope);
1172}
1173
1174//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001175// FenceInst Implementation
1176//===----------------------------------------------------------------------===//
1177
1178FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1179 SynchronizationScope SynchScope,
1180 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001181 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001182 setOrdering(Ordering);
1183 setSynchScope(SynchScope);
1184}
1185
1186FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1187 SynchronizationScope SynchScope,
1188 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001189 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001190 setOrdering(Ordering);
1191 setSynchScope(SynchScope);
1192}
1193
1194//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001195// GetElementPtrInst Implementation
1196//===----------------------------------------------------------------------===//
1197
Jay Foadd1b78492011-07-25 09:48:08 +00001198void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001199 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001200 assert(getNumOperands() == 1 + IdxList.size() &&
1201 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001202 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001203 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001204 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001205}
1206
Gabor Greiff6caff662008-05-10 08:32:32 +00001207GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001208 : Instruction(GEPI.getType(), GetElementPtr,
1209 OperandTraits<GetElementPtrInst>::op_end(this) -
1210 GEPI.getNumOperands(),
1211 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001212 SourceElementType(GEPI.SourceElementType),
1213 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001214 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001215 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001216}
1217
Chris Lattner6090a422009-03-09 04:46:40 +00001218/// getIndexedType - Returns the type of the element that would be accessed with
1219/// a gep instruction with the specified parameters.
1220///
1221/// The Idxs pointer should point to a continuous piece of memory containing the
1222/// indices, either as Value* or uint64_t.
1223///
1224/// A null type is returned if the indices are invalid for the specified
1225/// pointer type.
1226///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001227template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001228static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001229 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001230 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001231 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001232
Chris Lattner6090a422009-03-09 04:46:40 +00001233 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001234 // it cannot be 'stepped over'.
1235 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001236 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001237
Dan Gohman1ecaf452008-05-31 00:58:22 +00001238 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001239 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001240 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001241 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001242 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001243 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001244 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001245 }
Craig Topperc6207612014-04-09 06:08:46 +00001246 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001247}
1248
David Blaikied288fb82015-03-30 21:41:43 +00001249Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1250 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001251}
1252
David Blaikied288fb82015-03-30 21:41:43 +00001253Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001254 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001255 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001256}
1257
David Blaikied288fb82015-03-30 21:41:43 +00001258Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1259 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001260}
1261
Chris Lattner45f15572007-04-14 00:12:57 +00001262/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1263/// zeros. If so, the result pointer and the first operand have the same
1264/// value, just potentially different types.
1265bool GetElementPtrInst::hasAllZeroIndices() const {
1266 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1267 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1268 if (!CI->isZero()) return false;
1269 } else {
1270 return false;
1271 }
1272 }
1273 return true;
1274}
1275
Chris Lattner27058292007-04-27 20:35:56 +00001276/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1277/// constant integers. If so, the result pointer and the first operand have
1278/// a constant offset between them.
1279bool GetElementPtrInst::hasAllConstantIndices() const {
1280 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1281 if (!isa<ConstantInt>(getOperand(i)))
1282 return false;
1283 }
1284 return true;
1285}
1286
Dan Gohman1b849082009-09-07 23:54:19 +00001287void GetElementPtrInst::setIsInBounds(bool B) {
1288 cast<GEPOperator>(this)->setIsInBounds(B);
1289}
Chris Lattner45f15572007-04-14 00:12:57 +00001290
Nick Lewycky28a5f252009-09-27 21:33:04 +00001291bool GetElementPtrInst::isInBounds() const {
1292 return cast<GEPOperator>(this)->isInBounds();
1293}
1294
Chandler Carruth1e140532012-12-11 10:29:10 +00001295bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1296 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001297 // Delegate to the generic GEPOperator implementation.
1298 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001299}
1300
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001301//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001302// ExtractElementInst Implementation
1303//===----------------------------------------------------------------------===//
1304
1305ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001306 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001307 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001308 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001309 ExtractElement,
1310 OperandTraits<ExtractElementInst>::op_begin(this),
1311 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001312 assert(isValidOperands(Val, Index) &&
1313 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001314 Op<0>() = Val;
1315 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001316 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001317}
1318
1319ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001320 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001321 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001322 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001323 ExtractElement,
1324 OperandTraits<ExtractElementInst>::op_begin(this),
1325 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001326 assert(isValidOperands(Val, Index) &&
1327 "Invalid extractelement instruction operands!");
1328
Gabor Greif2d3024d2008-05-26 21:33:52 +00001329 Op<0>() = Val;
1330 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001331 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001332}
1333
Chris Lattner65511ff2006-10-05 06:24:58 +00001334
Chris Lattner54865b32006-04-08 04:05:48 +00001335bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001336 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001337 return false;
1338 return true;
1339}
1340
1341
Robert Bocchino23004482006-01-10 19:05:34 +00001342//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001343// InsertElementInst Implementation
1344//===----------------------------------------------------------------------===//
1345
Chris Lattner54865b32006-04-08 04:05:48 +00001346InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001347 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001348 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001349 : Instruction(Vec->getType(), InsertElement,
1350 OperandTraits<InsertElementInst>::op_begin(this),
1351 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001352 assert(isValidOperands(Vec, Elt, Index) &&
1353 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001354 Op<0>() = Vec;
1355 Op<1>() = Elt;
1356 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001357 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001358}
1359
Chris Lattner54865b32006-04-08 04:05:48 +00001360InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001361 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001362 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001363 : Instruction(Vec->getType(), InsertElement,
1364 OperandTraits<InsertElementInst>::op_begin(this),
1365 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001366 assert(isValidOperands(Vec, Elt, Index) &&
1367 "Invalid insertelement instruction operands!");
1368
Gabor Greif2d3024d2008-05-26 21:33:52 +00001369 Op<0>() = Vec;
1370 Op<1>() = Elt;
1371 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001372 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001373}
1374
Chris Lattner54865b32006-04-08 04:05:48 +00001375bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1376 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001377 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001378 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001379
Reid Spencerd84d35b2007-02-15 02:26:10 +00001380 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001381 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001382
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001383 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001384 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001385 return true;
1386}
1387
1388
Robert Bocchinoca27f032006-01-17 20:07:22 +00001389//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001390// ShuffleVectorInst Implementation
1391//===----------------------------------------------------------------------===//
1392
1393ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001394 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001395 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001396: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001397 cast<VectorType>(Mask->getType())->getNumElements()),
1398 ShuffleVector,
1399 OperandTraits<ShuffleVectorInst>::op_begin(this),
1400 OperandTraits<ShuffleVectorInst>::operands(this),
1401 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001402 assert(isValidOperands(V1, V2, Mask) &&
1403 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001404 Op<0>() = V1;
1405 Op<1>() = V2;
1406 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001407 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001408}
1409
1410ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001411 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001412 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001413: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1414 cast<VectorType>(Mask->getType())->getNumElements()),
1415 ShuffleVector,
1416 OperandTraits<ShuffleVectorInst>::op_begin(this),
1417 OperandTraits<ShuffleVectorInst>::operands(this),
1418 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001419 assert(isValidOperands(V1, V2, Mask) &&
1420 "Invalid shuffle vector instruction operands!");
1421
Gabor Greif2d3024d2008-05-26 21:33:52 +00001422 Op<0>() = V1;
1423 Op<1>() = V2;
1424 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001425 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001426}
1427
Mon P Wang25f01062008-11-10 04:46:22 +00001428bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001429 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001430 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001431 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001432 return false;
1433
Chris Lattner1dcb6542012-01-25 23:49:49 +00001434 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001435 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001436 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001437 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001438
1439 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001440 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1441 return true;
1442
Nate Begeman60a31c32010-08-13 00:16:46 +00001443 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001444 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001445 for (Value *Op : MV->operands()) {
1446 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001447 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001448 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001449 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001450 return false;
1451 }
1452 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001453 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001454 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001455
1456 if (const ConstantDataSequential *CDS =
1457 dyn_cast<ConstantDataSequential>(Mask)) {
1458 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1459 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1460 if (CDS->getElementAsInteger(i) >= V1Size*2)
1461 return false;
1462 return true;
1463 }
1464
1465 // The bitcode reader can create a place holder for a forward reference
1466 // used as the shuffle mask. When this occurs, the shuffle mask will
1467 // fall into this case and fail. To avoid this error, do this bit of
1468 // ugliness to allow such a mask pass.
1469 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1470 if (CE->getOpcode() == Instruction::UserOp1)
1471 return true;
1472
1473 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001474}
1475
Chris Lattnerf724e342008-03-02 05:28:33 +00001476/// getMaskValue - Return the index from the shuffle mask for the specified
1477/// output result. This is either -1 if the element is undef or a number less
1478/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001479int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1480 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1481 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001482 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001483 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001484 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001485 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001486 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001487}
1488
Chris Lattner1dcb6542012-01-25 23:49:49 +00001489/// getShuffleMask - Return the full mask for this instruction, where each
1490/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001491void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1492 SmallVectorImpl<int> &Result) {
1493 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001494
Chris Lattnercf129702012-01-26 02:51:13 +00001495 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001496 for (unsigned i = 0; i != NumElts; ++i)
1497 Result.push_back(CDS->getElementAsInteger(i));
1498 return;
1499 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001500 for (unsigned i = 0; i != NumElts; ++i) {
1501 Constant *C = Mask->getAggregateElement(i);
1502 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001503 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001504 }
1505}
1506
1507
Dan Gohman12fce772008-05-15 19:50:34 +00001508//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001509// InsertValueInst Class
1510//===----------------------------------------------------------------------===//
1511
Jay Foad57aa6362011-07-13 10:26:04 +00001512void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1513 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001514 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001515
1516 // There's no fundamental reason why we require at least one index
1517 // (other than weirdness with &*IdxBegin being invalid; see
1518 // getelementptr's init routine for example). But there's no
1519 // present need to support it.
1520 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1521
1522 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001523 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001524 Op<0>() = Agg;
1525 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001526
Jay Foad57aa6362011-07-13 10:26:04 +00001527 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001528 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001529}
1530
1531InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001532 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001533 OperandTraits<InsertValueInst>::op_begin(this), 2),
1534 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001535 Op<0>() = IVI.getOperand(0);
1536 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001537 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001538}
1539
1540//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001541// ExtractValueInst Class
1542//===----------------------------------------------------------------------===//
1543
Jay Foad57aa6362011-07-13 10:26:04 +00001544void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001545 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001546
Jay Foad57aa6362011-07-13 10:26:04 +00001547 // There's no fundamental reason why we require at least one index.
1548 // But there's no present need to support it.
1549 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001550
Jay Foad57aa6362011-07-13 10:26:04 +00001551 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001552 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001553}
1554
1555ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001556 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001557 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001558 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001559}
1560
Dan Gohman12fce772008-05-15 19:50:34 +00001561// getIndexedType - Returns the type of the element that would be extracted
1562// with an extractvalue instruction with the specified parameters.
1563//
1564// A null type is returned if the indices are invalid for the specified
1565// pointer type.
1566//
Chris Lattner229907c2011-07-18 04:54:35 +00001567Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001568 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001569 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001570 // We can't use CompositeType::indexValid(Index) here.
1571 // indexValid() always returns true for arrays because getelementptr allows
1572 // out-of-bounds indices. Since we don't allow those for extractvalue and
1573 // insertvalue we need to check array indexing manually.
1574 // Since the only other types we can index into are struct types it's just
1575 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001576 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001577 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001578 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001579 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001580 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001581 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001582 } else {
1583 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001584 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001585 }
1586
1587 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001588 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001589 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001590}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001591
1592//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001593// BinaryOperator Class
1594//===----------------------------------------------------------------------===//
1595
Chris Lattner2195fc42007-02-24 00:55:48 +00001596BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001597 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001598 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001599 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001600 OperandTraits<BinaryOperator>::op_begin(this),
1601 OperandTraits<BinaryOperator>::operands(this),
1602 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001603 Op<0>() = S1;
1604 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001605 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001606 setName(Name);
1607}
1608
1609BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001610 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001611 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001612 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001613 OperandTraits<BinaryOperator>::op_begin(this),
1614 OperandTraits<BinaryOperator>::operands(this),
1615 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001616 Op<0>() = S1;
1617 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001618 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001619 setName(Name);
1620}
1621
1622
1623void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001624 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001625 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001626 assert(LHS->getType() == RHS->getType() &&
1627 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001628#ifndef NDEBUG
1629 switch (iType) {
1630 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001631 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001632 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001633 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001634 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001635 "Tried to create an integer operation on a non-integer type!");
1636 break;
1637 case FAdd: case FSub:
1638 case FMul:
1639 assert(getType() == LHS->getType() &&
1640 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001641 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001642 "Tried to create a floating-point operation on a "
1643 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001644 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001645 case UDiv:
1646 case SDiv:
1647 assert(getType() == LHS->getType() &&
1648 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001649 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001650 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001651 "Incorrect operand type (not integer) for S/UDIV");
1652 break;
1653 case FDiv:
1654 assert(getType() == LHS->getType() &&
1655 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001656 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001657 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001658 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001659 case URem:
1660 case SRem:
1661 assert(getType() == LHS->getType() &&
1662 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001663 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001664 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001665 "Incorrect operand type (not integer) for S/UREM");
1666 break;
1667 case FRem:
1668 assert(getType() == LHS->getType() &&
1669 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001670 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001671 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001672 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001673 case Shl:
1674 case LShr:
1675 case AShr:
1676 assert(getType() == LHS->getType() &&
1677 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001678 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001679 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001680 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001681 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001682 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001683 case And: case Or:
1684 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001685 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001686 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001687 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001688 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001689 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001690 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001691 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001692 default:
1693 break;
1694 }
1695#endif
1696}
1697
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001698BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001699 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001700 Instruction *InsertBefore) {
1701 assert(S1->getType() == S2->getType() &&
1702 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001703 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001704}
1705
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001706BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001707 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001708 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001709 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001710 InsertAtEnd->getInstList().push_back(Res);
1711 return Res;
1712}
1713
Dan Gohman5476cfd2009-08-12 16:23:25 +00001714BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001715 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001716 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001717 return new BinaryOperator(Instruction::Sub,
1718 zero, Op,
1719 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001720}
1721
Dan Gohman5476cfd2009-08-12 16:23:25 +00001722BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001723 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001724 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001725 return new BinaryOperator(Instruction::Sub,
1726 zero, Op,
1727 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001728}
1729
Dan Gohman4ab44202009-12-18 02:58:50 +00001730BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1731 Instruction *InsertBefore) {
1732 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1733 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1734}
1735
1736BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1737 BasicBlock *InsertAtEnd) {
1738 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1739 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1740}
1741
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001742BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1743 Instruction *InsertBefore) {
1744 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1745 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1746}
1747
1748BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1749 BasicBlock *InsertAtEnd) {
1750 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1751 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1752}
1753
Dan Gohman5476cfd2009-08-12 16:23:25 +00001754BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001755 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001756 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001757 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001758 Op->getType(), Name, InsertBefore);
1759}
1760
Dan Gohman5476cfd2009-08-12 16:23:25 +00001761BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001762 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001763 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001764 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001765 Op->getType(), Name, InsertAtEnd);
1766}
1767
Dan Gohman5476cfd2009-08-12 16:23:25 +00001768BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001769 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001770 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001771 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001772 Op->getType(), Name, InsertBefore);
1773}
1774
Dan Gohman5476cfd2009-08-12 16:23:25 +00001775BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001776 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001777 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001778 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001779 Op->getType(), Name, InsertAtEnd);
1780}
1781
1782
1783// isConstantAllOnes - Helper function for several functions below
1784static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001785 if (const Constant *C = dyn_cast<Constant>(V))
1786 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001787 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001788}
1789
Owen Andersonbb2501b2009-07-13 22:18:28 +00001790bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001791 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1792 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001793 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1794 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001795 return false;
1796}
1797
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001798bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001799 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1800 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001801 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
1802 if (!IgnoreZeroSign)
1803 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
1804 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
1805 }
Dan Gohmana5b96452009-06-04 22:49:04 +00001806 return false;
1807}
1808
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001809bool BinaryOperator::isNot(const Value *V) {
1810 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1811 return (Bop->getOpcode() == Instruction::Xor &&
1812 (isConstantAllOnes(Bop->getOperand(1)) ||
1813 isConstantAllOnes(Bop->getOperand(0))));
1814 return false;
1815}
1816
Chris Lattner2c7d1772005-04-24 07:28:37 +00001817Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001818 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001819}
1820
Chris Lattner2c7d1772005-04-24 07:28:37 +00001821const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1822 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001823}
1824
Dan Gohmana5b96452009-06-04 22:49:04 +00001825Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001826 return cast<BinaryOperator>(BinOp)->getOperand(1);
1827}
1828
1829const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1830 return getFNegArgument(const_cast<Value*>(BinOp));
1831}
1832
Chris Lattner2c7d1772005-04-24 07:28:37 +00001833Value *BinaryOperator::getNotArgument(Value *BinOp) {
1834 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1835 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1836 Value *Op0 = BO->getOperand(0);
1837 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001838 if (isConstantAllOnes(Op0)) return Op1;
1839
1840 assert(isConstantAllOnes(Op1));
1841 return Op0;
1842}
1843
Chris Lattner2c7d1772005-04-24 07:28:37 +00001844const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1845 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001846}
1847
1848
1849// swapOperands - Exchange the two operands to this instruction. This
1850// instruction is safe to use on any binary instruction and does not
1851// modify the semantics of the instruction. If the instruction is
1852// order dependent (SetLT f.e.) the opcode is changed.
1853//
1854bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001855 if (!isCommutative())
1856 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001857 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001858 return false;
1859}
1860
Dan Gohman1b849082009-09-07 23:54:19 +00001861void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1862 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1863}
1864
1865void BinaryOperator::setHasNoSignedWrap(bool b) {
1866 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1867}
1868
1869void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00001870 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00001871}
1872
Nick Lewycky28a5f252009-09-27 21:33:04 +00001873bool BinaryOperator::hasNoUnsignedWrap() const {
1874 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1875}
1876
1877bool BinaryOperator::hasNoSignedWrap() const {
1878 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1879}
1880
1881bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00001882 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00001883}
1884
Sanjay Patela982d992014-09-03 01:06:50 +00001885void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00001886 // Copy the wrapping flags.
1887 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
1888 setHasNoSignedWrap(OB->hasNoSignedWrap());
1889 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
1890 }
1891
1892 // Copy the exact flag.
1893 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
1894 setIsExact(PE->isExact());
1895
1896 // Copy the fast-math flags.
1897 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00001898 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00001899}
1900
Sanjay Patela982d992014-09-03 01:06:50 +00001901void BinaryOperator::andIRFlags(const Value *V) {
1902 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
1903 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
1904 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
1905 }
1906
1907 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
1908 setIsExact(isExact() & PE->isExact());
1909
1910 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
1911 FastMathFlags FM = getFastMathFlags();
1912 FM &= FP->getFastMathFlags();
1913 copyFastMathFlags(FM);
1914 }
1915}
1916
1917
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001918//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00001919// FPMathOperator Class
1920//===----------------------------------------------------------------------===//
1921
1922/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
1923/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00001924/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00001925float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001926 const MDNode *MD =
1927 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00001928 if (!MD)
1929 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001930 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00001931 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00001932}
1933
1934
1935//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001936// CastInst Class
1937//===----------------------------------------------------------------------===//
1938
David Blaikie3a15e142011-12-01 08:00:17 +00001939void CastInst::anchor() {}
1940
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001941// Just determine if this cast only deals with integral->integral conversion.
1942bool CastInst::isIntegerCast() const {
1943 switch (getOpcode()) {
1944 default: return false;
1945 case Instruction::ZExt:
1946 case Instruction::SExt:
1947 case Instruction::Trunc:
1948 return true;
1949 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001950 return getOperand(0)->getType()->isIntegerTy() &&
1951 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001952 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001953}
1954
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001955bool CastInst::isLosslessCast() const {
1956 // Only BitCast can be lossless, exit fast if we're not BitCast
1957 if (getOpcode() != Instruction::BitCast)
1958 return false;
1959
1960 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00001961 Type* SrcTy = getOperand(0)->getType();
1962 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001963 if (SrcTy == DstTy)
1964 return true;
1965
Reid Spencer8d9336d2006-12-31 05:26:44 +00001966 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001967 if (SrcTy->isPointerTy())
1968 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001969 return false; // Other types have no identity values
1970}
1971
1972/// This function determines if the CastInst does not require any bits to be
1973/// changed in order to effect the cast. Essentially, it identifies cases where
1974/// no code gen is necessary for the cast, hence the name no-op cast. For
1975/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001976/// # bitcast i32* %x to i8*
1977/// # bitcast <2 x i32> %x to <4 x i16>
1978/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001979/// @brief Determine if the described cast is a no-op.
1980bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00001981 Type *SrcTy,
1982 Type *DestTy,
1983 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001984 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00001985 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001986 case Instruction::Trunc:
1987 case Instruction::ZExt:
1988 case Instruction::SExt:
1989 case Instruction::FPTrunc:
1990 case Instruction::FPExt:
1991 case Instruction::UIToFP:
1992 case Instruction::SIToFP:
1993 case Instruction::FPToUI:
1994 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001995 case Instruction::AddrSpaceCast:
1996 // TODO: Target informations may give a more accurate answer here.
1997 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001998 case Instruction::BitCast:
1999 return true; // BitCast never modifies bits.
2000 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002001 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002002 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002004 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002005 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002006 }
2007}
2008
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002009/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002010bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002011 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2012}
2013
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002014bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002015 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002016 if (getOpcode() == Instruction::PtrToInt)
2017 PtrOpTy = getOperand(0)->getType();
2018 else if (getOpcode() == Instruction::IntToPtr)
2019 PtrOpTy = getType();
2020
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002021 Type *IntPtrTy =
2022 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002023
2024 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2025}
2026
2027/// This function determines if a pair of casts can be eliminated and what
2028/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002029/// instructions like this:
2030/// * %F = firstOpcode SrcTy %x to MidTy
2031/// * %S = secondOpcode MidTy %F to DstTy
2032/// The function returns a resultOpcode so these two casts can be replaced with:
2033/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2034/// If no such cast is permited, the function returns 0.
2035unsigned CastInst::isEliminableCastPair(
2036 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002037 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2038 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002039 // Define the 144 possibilities for these two cast instructions. The values
2040 // in this matrix determine what to do in a given situation and select the
2041 // case in the switch below. The rows correspond to firstOp, the columns
2042 // correspond to secondOp. In looking at the table below, keep in mind
2043 // the following cast properties:
2044 //
2045 // Size Compare Source Destination
2046 // Operator Src ? Size Type Sign Type Sign
2047 // -------- ------------ ------------------- ---------------------
2048 // TRUNC > Integer Any Integral Any
2049 // ZEXT < Integral Unsigned Integer Any
2050 // SEXT < Integral Signed Integer Any
2051 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002052 // FPTOSI n/a FloatPt n/a Integral Signed
2053 // UITOFP n/a Integral Unsigned FloatPt n/a
2054 // SITOFP n/a Integral Signed FloatPt n/a
2055 // FPTRUNC > FloatPt n/a FloatPt n/a
2056 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002057 // PTRTOINT n/a Pointer n/a Integral Unsigned
2058 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002059 // BITCAST = FirstClass n/a FirstClass n/a
2060 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002061 //
2062 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002063 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2064 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002065 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002066 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002067 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002068 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002069 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002070 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2071 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002072 // T F F U S F F P I B A -+
2073 // R Z S P P I I T P 2 N T S |
2074 // U E E 2 2 2 2 R E I T C C +- secondOp
2075 // N X X U S F F N X N 2 V V |
2076 // C T T I I P P C T T P T T -+
2077 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002078 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002079 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2080 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2081 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2082 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2083 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002084 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002085 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2086 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2087 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2088 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2089 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002090 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002091
Chris Lattner25eea4d2010-07-12 01:19:22 +00002092 // If either of the casts are a bitcast from scalar to vector, disallow the
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002093 // merging. However, bitcast of A->B->A are allowed.
2094 bool isFirstBitcast = (firstOp == Instruction::BitCast);
2095 bool isSecondBitcast = (secondOp == Instruction::BitCast);
2096 bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2097
2098 // Check if any of the bitcasts convert scalars<->vectors.
2099 if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2100 (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2101 // Unless we are bitcasing to the original type, disallow optimizations.
2102 if (!chainedBitcast) return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103
2104 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2105 [secondOp-Instruction::CastOpsBegin];
2106 switch (ElimCase) {
2107 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002108 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002109 return 0;
2110 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002111 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112 return firstOp;
2113 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002114 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115 return secondOp;
2116 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002117 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002118 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002119 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002120 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121 return firstOp;
2122 return 0;
2123 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002124 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002125 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002126 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002127 return firstOp;
2128 return 0;
2129 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002130 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002131 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002132 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002133 return secondOp;
2134 return 0;
2135 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002136 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002137 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002138 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002139 return secondOp;
2140 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002141 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002142 // Cannot simplify if address spaces are different!
2143 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2144 return 0;
2145
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002146 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002147 // We can still fold this without knowing the actual sizes as long we
2148 // know that the intermediate pointer is the largest possible
2149 // pointer size.
2150 // FIXME: Is this always true?
2151 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002152 return Instruction::BitCast;
2153
2154 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002155 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002156 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002157 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002158 if (MidSize >= PtrSize)
2159 return Instruction::BitCast;
2160 return 0;
2161 }
2162 case 8: {
2163 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2164 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2165 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002166 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2167 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002168 if (SrcSize == DstSize)
2169 return Instruction::BitCast;
2170 else if (SrcSize < DstSize)
2171 return firstOp;
2172 return secondOp;
2173 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002174 case 9:
2175 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176 return Instruction::ZExt;
2177 case 10:
2178 // fpext followed by ftrunc is allowed if the bit size returned to is
2179 // the same as the original, in which case its just a bitcast
2180 if (SrcTy == DstTy)
2181 return Instruction::BitCast;
2182 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002183 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002184 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002185 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002186 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002187 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002188 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2189 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190 if (SrcSize <= PtrSize && SrcSize == DstSize)
2191 return Instruction::BitCast;
2192 return 0;
2193 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002194 case 12: {
2195 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2196 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2197 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2198 return Instruction::AddrSpaceCast;
2199 return Instruction::BitCast;
2200 }
2201 case 13:
2202 // FIXME: this state can be merged with (1), but the following assert
2203 // is useful to check the correcteness of the sequence due to semantic
2204 // change of bitcast.
2205 assert(
2206 SrcTy->isPtrOrPtrVectorTy() &&
2207 MidTy->isPtrOrPtrVectorTy() &&
2208 DstTy->isPtrOrPtrVectorTy() &&
2209 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2210 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2211 "Illegal addrspacecast, bitcast sequence!");
2212 // Allowed, use first cast's opcode
2213 return firstOp;
2214 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002215 // bitcast, addrspacecast -> addrspacecast if the element type of
2216 // bitcast's source is the same as that of addrspacecast's destination.
2217 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2218 return Instruction::AddrSpaceCast;
2219 return 0;
2220
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002221 case 15:
2222 // FIXME: this state can be merged with (1), but the following assert
2223 // is useful to check the correcteness of the sequence due to semantic
2224 // change of bitcast.
2225 assert(
2226 SrcTy->isIntOrIntVectorTy() &&
2227 MidTy->isPtrOrPtrVectorTy() &&
2228 DstTy->isPtrOrPtrVectorTy() &&
2229 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2230 "Illegal inttoptr, bitcast sequence!");
2231 // Allowed, use first cast's opcode
2232 return firstOp;
2233 case 16:
2234 // FIXME: this state can be merged with (2), but the following assert
2235 // is useful to check the correcteness of the sequence due to semantic
2236 // change of bitcast.
2237 assert(
2238 SrcTy->isPtrOrPtrVectorTy() &&
2239 MidTy->isPtrOrPtrVectorTy() &&
2240 DstTy->isIntOrIntVectorTy() &&
2241 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2242 "Illegal bitcast, ptrtoint sequence!");
2243 // Allowed, use second cast's opcode
2244 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002245 case 17:
2246 // (sitofp (zext x)) -> (uitofp x)
2247 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002249 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002251 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252 default:
Craig Topperc514b542012-02-05 22:14:15 +00002253 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002254 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002255}
2256
Chris Lattner229907c2011-07-18 04:54:35 +00002257CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002258 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002259 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002260 // Construct and return the appropriate CastInst subclass
2261 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002262 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2263 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2264 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2265 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2266 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2267 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2268 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2269 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2270 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2271 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2272 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2273 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2274 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2275 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002276 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002277}
2278
Chris Lattner229907c2011-07-18 04:54:35 +00002279CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002280 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002281 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002282 // Construct and return the appropriate CastInst subclass
2283 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002284 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2285 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2286 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2287 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2288 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2289 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2290 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2291 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2292 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2293 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2294 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2295 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2296 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2297 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002298 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299}
2300
Chris Lattner229907c2011-07-18 04:54:35 +00002301CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002302 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002303 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002304 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002305 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2306 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002307}
2308
Chris Lattner229907c2011-07-18 04:54:35 +00002309CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002310 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002311 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002312 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002313 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2314 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002315}
2316
Chris Lattner229907c2011-07-18 04:54:35 +00002317CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002318 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002319 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002320 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002321 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2322 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002323}
2324
Chris Lattner229907c2011-07-18 04:54:35 +00002325CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002326 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002327 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002328 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002329 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2330 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002331}
2332
Chris Lattner229907c2011-07-18 04:54:35 +00002333CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002334 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002335 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002336 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002337 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2338 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002339}
2340
Chris Lattner229907c2011-07-18 04:54:35 +00002341CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002342 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002343 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002344 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002345 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2346 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002347}
2348
Chris Lattner229907c2011-07-18 04:54:35 +00002349CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002350 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002351 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002352 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2353 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2354 "Invalid cast");
2355 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002356 assert((!Ty->isVectorTy() ||
2357 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002358 "Invalid cast");
2359
Matt Arsenault065ced92013-07-31 00:17:33 +00002360 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002361 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002362
Matt Arsenault740980e2014-07-14 17:24:35 +00002363 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002364}
2365
2366/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002367CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2368 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002369 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002370 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2371 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002372 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002373 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002374 assert((!Ty->isVectorTy() ||
2375 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002376 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002377
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002378 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002379 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002380
Matt Arsenault740980e2014-07-14 17:24:35 +00002381 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2382}
2383
2384CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2385 Value *S, Type *Ty,
2386 const Twine &Name,
2387 BasicBlock *InsertAtEnd) {
2388 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2389 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2390
2391 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2392 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2393
2394 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2395}
2396
2397CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2398 Value *S, Type *Ty,
2399 const Twine &Name,
2400 Instruction *InsertBefore) {
2401 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2402 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2403
2404 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002405 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2406
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002407 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002408}
2409
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002410CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2411 const Twine &Name,
2412 Instruction *InsertBefore) {
2413 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2414 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2415 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2416 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2417
2418 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2419}
2420
Matt Arsenault740980e2014-07-14 17:24:35 +00002421CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002422 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002423 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002424 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002425 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002426 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2427 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002428 Instruction::CastOps opcode =
2429 (SrcBits == DstBits ? Instruction::BitCast :
2430 (SrcBits > DstBits ? Instruction::Trunc :
2431 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002432 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002433}
2434
Chris Lattner229907c2011-07-18 04:54:35 +00002435CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002436 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002437 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002438 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002439 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002440 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2441 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002442 Instruction::CastOps opcode =
2443 (SrcBits == DstBits ? Instruction::BitCast :
2444 (SrcBits > DstBits ? Instruction::Trunc :
2445 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002446 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002447}
2448
Chris Lattner229907c2011-07-18 04:54:35 +00002449CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002450 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002451 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002452 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002453 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002454 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2455 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002456 Instruction::CastOps opcode =
2457 (SrcBits == DstBits ? Instruction::BitCast :
2458 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002459 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002460}
2461
Chris Lattner229907c2011-07-18 04:54:35 +00002462CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002463 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002464 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002465 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002466 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002467 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2468 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002469 Instruction::CastOps opcode =
2470 (SrcBits == DstBits ? Instruction::BitCast :
2471 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002472 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002473}
2474
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002475// Check whether it is valid to call getCastOpcode for these types.
2476// This routine must be kept in sync with getCastOpcode.
2477bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2478 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2479 return false;
2480
2481 if (SrcTy == DestTy)
2482 return true;
2483
2484 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2485 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2486 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2487 // An element by element cast. Valid if casting the elements is valid.
2488 SrcTy = SrcVecTy->getElementType();
2489 DestTy = DestVecTy->getElementType();
2490 }
2491
2492 // Get the bit sizes, we'll need these
2493 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2494 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2495
2496 // Run through the possibilities ...
2497 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002498 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002499 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002500 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002501 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002502 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002503 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002504 // Casting from something else
2505 return SrcTy->isPointerTy();
2506 }
2507 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2508 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002509 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002510 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002511 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002512 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002513 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002514 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002515 return false;
2516 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002517 if (DestTy->isVectorTy()) // Casting to vector
2518 return DestBits == SrcBits;
2519 if (DestTy->isPointerTy()) { // Casting to pointer
2520 if (SrcTy->isPointerTy()) // Casting from pointer
2521 return true;
2522 return SrcTy->isIntegerTy(); // Casting from integral
2523 }
2524 if (DestTy->isX86_MMXTy()) {
2525 if (SrcTy->isVectorTy())
2526 return DestBits == SrcBits; // 64-bit vector to MMX
2527 return false;
2528 } // Casting to something else
2529 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002530}
2531
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002532bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2533 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2534 return false;
2535
2536 if (SrcTy == DestTy)
2537 return true;
2538
2539 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2540 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2541 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2542 // An element by element cast. Valid if casting the elements is valid.
2543 SrcTy = SrcVecTy->getElementType();
2544 DestTy = DestVecTy->getElementType();
2545 }
2546 }
2547 }
2548
2549 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2550 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2551 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2552 }
2553 }
2554
2555 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2556 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2557
2558 // Could still have vectors of pointers if the number of elements doesn't
2559 // match
2560 if (SrcBits == 0 || DestBits == 0)
2561 return false;
2562
2563 if (SrcBits != DestBits)
2564 return false;
2565
2566 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2567 return false;
2568
2569 return true;
2570}
2571
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002572bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002573 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002574 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2575 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002576 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002577 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2578 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002579 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002580
2581 return isBitCastable(SrcTy, DestTy);
2582}
2583
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002584// Provide a way to get a "cast" where the cast opcode is inferred from the
2585// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002586// logic in the castIsValid function below. This axiom should hold:
2587// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2588// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002590// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002592CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002593 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2594 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595
Duncan Sands55e50902008-01-06 10:12:28 +00002596 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2597 "Only first class types are castable!");
2598
Duncan Sandsa8514532011-05-18 07:13:41 +00002599 if (SrcTy == DestTy)
2600 return BitCast;
2601
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002602 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002603 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2604 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002605 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2606 // An element by element cast. Find the appropriate opcode based on the
2607 // element types.
2608 SrcTy = SrcVecTy->getElementType();
2609 DestTy = DestVecTy->getElementType();
2610 }
2611
2612 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002613 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2614 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002615
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002617 if (DestTy->isIntegerTy()) { // Casting to integral
2618 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619 if (DestBits < SrcBits)
2620 return Trunc; // int -> smaller int
2621 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002622 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623 return SExt; // signed -> SEXT
2624 else
2625 return ZExt; // unsigned -> ZEXT
2626 } else {
2627 return BitCast; // Same size, No-op cast
2628 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002629 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002630 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631 return FPToSI; // FP -> sint
2632 else
2633 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002634 } else if (SrcTy->isVectorTy()) {
2635 assert(DestBits == SrcBits &&
2636 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637 return BitCast; // Same size, no-op cast
2638 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002639 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002640 "Casting from a value that is not first-class type");
2641 return PtrToInt; // ptr -> int
2642 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002643 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2644 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002645 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646 return SIToFP; // sint -> FP
2647 else
2648 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002649 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002650 if (DestBits < SrcBits) {
2651 return FPTrunc; // FP -> smaller FP
2652 } else if (DestBits > SrcBits) {
2653 return FPExt; // FP -> larger FP
2654 } else {
2655 return BitCast; // same size, no-op cast
2656 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002657 } else if (SrcTy->isVectorTy()) {
2658 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002659 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002660 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002662 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002663 } else if (DestTy->isVectorTy()) {
2664 assert(DestBits == SrcBits &&
2665 "Illegal cast to vector (wrong type or size)");
2666 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002667 } else if (DestTy->isPointerTy()) {
2668 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002669 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2670 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002672 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002673 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002675 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002676 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002677 if (SrcTy->isVectorTy()) {
2678 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002679 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002680 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002681 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002683 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002684}
2685
2686//===----------------------------------------------------------------------===//
2687// CastInst SubClass Constructors
2688//===----------------------------------------------------------------------===//
2689
2690/// Check that the construction parameters for a CastInst are correct. This
2691/// could be broken out into the separate constructors but it is useful to have
2692/// it in one place and to eliminate the redundant code for getting the sizes
2693/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002694bool
Chris Lattner229907c2011-07-18 04:54:35 +00002695CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002696
2697 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002698 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002699
Chris Lattner37bc78a2010-01-26 21:51:43 +00002700 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2701 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002702 return false;
2703
2704 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002705 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2706 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002707
Duncan Sands7f646562011-05-18 09:21:57 +00002708 // If these are vector types, get the lengths of the vectors (using zero for
2709 // scalar types means that checking that vector lengths match also checks that
2710 // scalars are not being converted to vectors or vectors to scalars).
2711 unsigned SrcLength = SrcTy->isVectorTy() ?
2712 cast<VectorType>(SrcTy)->getNumElements() : 0;
2713 unsigned DstLength = DstTy->isVectorTy() ?
2714 cast<VectorType>(DstTy)->getNumElements() : 0;
2715
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002716 // Switch on the opcode provided
2717 switch (op) {
2718 default: return false; // This is an input error
2719 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002720 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2721 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002722 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002723 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2724 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002725 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002726 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2727 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002728 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002729 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2730 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002731 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002732 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2733 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002734 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002735 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002736 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2737 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002738 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002740 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2741 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002742 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002743 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002744 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002745 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2746 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2747 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002748 return SrcTy->getScalarType()->isPointerTy() &&
2749 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002750 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002751 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002752 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002753 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2754 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2755 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00002756 return SrcTy->getScalarType()->isIntegerTy() &&
2757 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002758 case Instruction::BitCast: {
2759 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2760 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2761
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002762 // BitCast implies a no-op cast of type only. No bits change.
2763 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002764 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002765 return false;
2766
Alp Tokerf907b892013-12-05 05:44:44 +00002767 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002768 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002769 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002770 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2771
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002772 // If both are pointers then the address spaces must match.
2773 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
2774 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002775
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002776 // A vector of pointers must have the same number of elements.
2777 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2778 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2779 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2780
2781 return false;
2782 }
2783
2784 return true;
2785 }
2786 case Instruction::AddrSpaceCast: {
2787 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2788 if (!SrcPtrTy)
2789 return false;
2790
2791 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2792 if (!DstPtrTy)
2793 return false;
2794
2795 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
2796 return false;
2797
2798 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2799 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2800 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2801
2802 return false;
2803 }
2804
2805 return true;
2806 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002807 }
2808}
2809
2810TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002811 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002812) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002813 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002814}
2815
2816TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002817 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002818) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002819 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002820}
2821
2822ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002823 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002824) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002825 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002826}
2827
2828ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002829 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002830) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002831 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002832}
2833SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002834 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002835) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002836 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002837}
2838
Jeff Cohencc08c832006-12-02 02:22:01 +00002839SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002840 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002841) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002842 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002843}
2844
2845FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002846 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002847) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002848 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002849}
2850
2851FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002852 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002853) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002854 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002855}
2856
2857FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002858 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002859) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002860 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002861}
2862
2863FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002864 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002865) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002866 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002867}
2868
2869UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002870 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002871) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002872 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002873}
2874
2875UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002876 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002877) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002878 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002879}
2880
2881SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002882 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002883) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002884 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002885}
2886
2887SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002888 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002889) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002890 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002891}
2892
2893FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002894 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002895) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002896 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002897}
2898
2899FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002900 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002901) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002902 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002903}
2904
2905FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002906 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002907) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002908 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002909}
2910
2911FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002912 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002913) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002914 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002915}
2916
2917PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002918 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002919) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002920 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002921}
2922
2923PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002924 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002925) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002926 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002927}
2928
2929IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002930 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002931) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002932 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002933}
2934
2935IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002936 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002937) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002938 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002939}
2940
2941BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002942 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002943) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002944 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945}
2946
2947BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002948 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002950 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002951}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002952
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002953AddrSpaceCastInst::AddrSpaceCastInst(
2954 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2955) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
2956 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
2957}
2958
2959AddrSpaceCastInst::AddrSpaceCastInst(
2960 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2961) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
2962 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
2963}
2964
Chris Lattnerf16dc002006-09-17 19:29:56 +00002965//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002966// CmpInst Classes
2967//===----------------------------------------------------------------------===//
2968
Craig Topper3186c012012-09-23 02:12:10 +00002969void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00002970
Chris Lattner229907c2011-07-18 04:54:35 +00002971CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002972 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002973 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002974 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002975 OperandTraits<CmpInst>::op_begin(this),
2976 OperandTraits<CmpInst>::operands(this),
2977 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002978 Op<0>() = LHS;
2979 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002980 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002981 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002982}
Gabor Greiff6caff662008-05-10 08:32:32 +00002983
Chris Lattner229907c2011-07-18 04:54:35 +00002984CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002985 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002986 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002987 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002988 OperandTraits<CmpInst>::op_begin(this),
2989 OperandTraits<CmpInst>::operands(this),
2990 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002991 Op<0>() = LHS;
2992 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002993 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002994 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002995}
2996
2997CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002998CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002999 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003000 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003001 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003002 if (InsertBefore)
3003 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3004 S1, S2, Name);
3005 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003006 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003007 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003008 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003009
3010 if (InsertBefore)
3011 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3012 S1, S2, Name);
3013 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003014 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003015 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003016}
3017
3018CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00003019CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003020 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003021 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003022 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3023 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003024 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003025 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3026 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003027}
3028
3029void CmpInst::swapOperands() {
3030 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3031 IC->swapOperands();
3032 else
3033 cast<FCmpInst>(this)->swapOperands();
3034}
3035
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003036bool CmpInst::isCommutative() const {
3037 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003038 return IC->isCommutative();
3039 return cast<FCmpInst>(this)->isCommutative();
3040}
3041
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003042bool CmpInst::isEquality() const {
3043 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003044 return IC->isEquality();
3045 return cast<FCmpInst>(this)->isEquality();
3046}
3047
3048
Dan Gohman4e724382008-05-31 02:47:54 +00003049CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003050 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003051 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003052 case ICMP_EQ: return ICMP_NE;
3053 case ICMP_NE: return ICMP_EQ;
3054 case ICMP_UGT: return ICMP_ULE;
3055 case ICMP_ULT: return ICMP_UGE;
3056 case ICMP_UGE: return ICMP_ULT;
3057 case ICMP_ULE: return ICMP_UGT;
3058 case ICMP_SGT: return ICMP_SLE;
3059 case ICMP_SLT: return ICMP_SGE;
3060 case ICMP_SGE: return ICMP_SLT;
3061 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003062
Dan Gohman4e724382008-05-31 02:47:54 +00003063 case FCMP_OEQ: return FCMP_UNE;
3064 case FCMP_ONE: return FCMP_UEQ;
3065 case FCMP_OGT: return FCMP_ULE;
3066 case FCMP_OLT: return FCMP_UGE;
3067 case FCMP_OGE: return FCMP_ULT;
3068 case FCMP_OLE: return FCMP_UGT;
3069 case FCMP_UEQ: return FCMP_ONE;
3070 case FCMP_UNE: return FCMP_OEQ;
3071 case FCMP_UGT: return FCMP_OLE;
3072 case FCMP_ULT: return FCMP_OGE;
3073 case FCMP_UGE: return FCMP_OLT;
3074 case FCMP_ULE: return FCMP_OGT;
3075 case FCMP_ORD: return FCMP_UNO;
3076 case FCMP_UNO: return FCMP_ORD;
3077 case FCMP_TRUE: return FCMP_FALSE;
3078 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003079 }
3080}
3081
Reid Spencer266e42b2006-12-23 06:05:41 +00003082ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3083 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003084 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003085 case ICMP_EQ: case ICMP_NE:
3086 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3087 return pred;
3088 case ICMP_UGT: return ICMP_SGT;
3089 case ICMP_ULT: return ICMP_SLT;
3090 case ICMP_UGE: return ICMP_SGE;
3091 case ICMP_ULE: return ICMP_SLE;
3092 }
3093}
3094
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003095ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3096 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003097 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003098 case ICMP_EQ: case ICMP_NE:
3099 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3100 return pred;
3101 case ICMP_SGT: return ICMP_UGT;
3102 case ICMP_SLT: return ICMP_ULT;
3103 case ICMP_SGE: return ICMP_UGE;
3104 case ICMP_SLE: return ICMP_ULE;
3105 }
3106}
3107
Reid Spencer0286bc12007-02-28 22:00:54 +00003108/// Initialize a set of values that all satisfy the condition with C.
3109///
3110ConstantRange
3111ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3112 APInt Lower(C);
3113 APInt Upper(C);
3114 uint32_t BitWidth = C.getBitWidth();
3115 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003116 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003117 case ICmpInst::ICMP_EQ: ++Upper; break;
3118 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003119 case ICmpInst::ICMP_ULT:
3120 Lower = APInt::getMinValue(BitWidth);
3121 // Check for an empty-set condition.
3122 if (Lower == Upper)
3123 return ConstantRange(BitWidth, /*isFullSet=*/false);
3124 break;
3125 case ICmpInst::ICMP_SLT:
3126 Lower = APInt::getSignedMinValue(BitWidth);
3127 // Check for an empty-set condition.
3128 if (Lower == Upper)
3129 return ConstantRange(BitWidth, /*isFullSet=*/false);
3130 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003131 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003132 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003133 // Check for an empty-set condition.
3134 if (Lower == Upper)
3135 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003136 break;
3137 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003138 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003139 // Check for an empty-set condition.
3140 if (Lower == Upper)
3141 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003142 break;
3143 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003144 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003145 // Check for a full-set condition.
3146 if (Lower == Upper)
3147 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003148 break;
3149 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003150 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003151 // Check for a full-set condition.
3152 if (Lower == Upper)
3153 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003154 break;
3155 case ICmpInst::ICMP_UGE:
3156 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003157 // Check for a full-set condition.
3158 if (Lower == Upper)
3159 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003160 break;
3161 case ICmpInst::ICMP_SGE:
3162 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003163 // Check for a full-set condition.
3164 if (Lower == Upper)
3165 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003166 break;
3167 }
3168 return ConstantRange(Lower, Upper);
3169}
3170
Dan Gohman4e724382008-05-31 02:47:54 +00003171CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003172 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003173 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003174 case ICMP_EQ: case ICMP_NE:
3175 return pred;
3176 case ICMP_SGT: return ICMP_SLT;
3177 case ICMP_SLT: return ICMP_SGT;
3178 case ICMP_SGE: return ICMP_SLE;
3179 case ICMP_SLE: return ICMP_SGE;
3180 case ICMP_UGT: return ICMP_ULT;
3181 case ICMP_ULT: return ICMP_UGT;
3182 case ICMP_UGE: return ICMP_ULE;
3183 case ICMP_ULE: return ICMP_UGE;
3184
Reid Spencerd9436b62006-11-20 01:22:35 +00003185 case FCMP_FALSE: case FCMP_TRUE:
3186 case FCMP_OEQ: case FCMP_ONE:
3187 case FCMP_UEQ: case FCMP_UNE:
3188 case FCMP_ORD: case FCMP_UNO:
3189 return pred;
3190 case FCMP_OGT: return FCMP_OLT;
3191 case FCMP_OLT: return FCMP_OGT;
3192 case FCMP_OGE: return FCMP_OLE;
3193 case FCMP_OLE: return FCMP_OGE;
3194 case FCMP_UGT: return FCMP_ULT;
3195 case FCMP_ULT: return FCMP_UGT;
3196 case FCMP_UGE: return FCMP_ULE;
3197 case FCMP_ULE: return FCMP_UGE;
3198 }
3199}
3200
Reid Spencer266e42b2006-12-23 06:05:41 +00003201bool CmpInst::isUnsigned(unsigned short predicate) {
3202 switch (predicate) {
3203 default: return false;
3204 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3205 case ICmpInst::ICMP_UGE: return true;
3206 }
3207}
3208
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003209bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003210 switch (predicate) {
3211 default: return false;
3212 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3213 case ICmpInst::ICMP_SGE: return true;
3214 }
3215}
3216
3217bool CmpInst::isOrdered(unsigned short predicate) {
3218 switch (predicate) {
3219 default: return false;
3220 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3221 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3222 case FCmpInst::FCMP_ORD: return true;
3223 }
3224}
3225
3226bool CmpInst::isUnordered(unsigned short predicate) {
3227 switch (predicate) {
3228 default: return false;
3229 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3230 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3231 case FCmpInst::FCMP_UNO: return true;
3232 }
3233}
3234
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003235bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3236 switch(predicate) {
3237 default: return false;
3238 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3239 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3240 }
3241}
3242
3243bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3244 switch(predicate) {
3245 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3246 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3247 default: return false;
3248 }
3249}
3250
3251
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003252//===----------------------------------------------------------------------===//
3253// SwitchInst Implementation
3254//===----------------------------------------------------------------------===//
3255
Chris Lattnerbaf00152010-11-17 05:41:46 +00003256void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3257 assert(Value && Default && NumReserved);
3258 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003259 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003260 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003261
Pete Coopereb31b682015-05-21 22:48:54 +00003262 Op<0>() = Value;
3263 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003264}
3265
Chris Lattner2195fc42007-02-24 00:55:48 +00003266/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3267/// switch on and a default destination. The number of additional cases can
3268/// be specified here to make memory allocation more efficient. This
3269/// constructor can also autoinsert before another instruction.
3270SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3271 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003272 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003273 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003274 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003275}
3276
3277/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3278/// switch on and a default destination. The number of additional cases can
3279/// be specified here to make memory allocation more efficient. This
3280/// constructor also autoinserts at the end of the specified BasicBlock.
3281SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3282 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003283 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003284 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003285 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003286}
3287
Misha Brukmanb1c93172005-04-21 23:48:37 +00003288SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003289 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003290 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003291 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003292 Use *OL = getOperandList();
3293 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003294 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003295 OL[i] = InOL[i];
3296 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003297 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003298 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003299}
3300
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003301
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003302/// addCase - Add an entry to the switch instruction...
3303///
Chris Lattner47ac1872005-02-24 05:32:09 +00003304void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003305 unsigned NewCaseIdx = getNumCases();
3306 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003307 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003308 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003309 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003310 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003311 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003312 CaseIt Case(this, NewCaseIdx);
3313 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003314 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003315}
3316
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003317/// removeCase - This method removes the specified case and its successor
3318/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003319void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003320 unsigned idx = i.getCaseIndex();
3321
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003322 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003323
3324 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003325 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003326
Jay Foad14277722011-02-01 09:22:34 +00003327 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003328 if (2 + (idx + 1) * 2 != NumOps) {
3329 OL[2 + idx * 2] = OL[NumOps - 2];
3330 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003331 }
3332
3333 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003334 OL[NumOps-2].set(nullptr);
3335 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003336 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003337}
3338
Jay Foade98f29d2011-04-01 08:00:58 +00003339/// growOperands - grow operands - This grows the operand list in response
3340/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003341///
Jay Foade98f29d2011-04-01 08:00:58 +00003342void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003343 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003344 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003345
3346 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003347 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003348}
3349
3350
3351BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3352 return getSuccessor(idx);
3353}
3354unsigned SwitchInst::getNumSuccessorsV() const {
3355 return getNumSuccessors();
3356}
3357void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3358 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003359}
Chris Lattnerf22be932004-10-15 23:52:53 +00003360
Chris Lattner3ed871f2009-10-27 19:13:16 +00003361//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003362// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003363//===----------------------------------------------------------------------===//
3364
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003365void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003366 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003367 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003368 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003369 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003370 allocHungoffUses(ReservedSpace);
3371
Pete Coopereb31b682015-05-21 22:48:54 +00003372 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003373}
3374
3375
Jay Foade98f29d2011-04-01 08:00:58 +00003376/// growOperands - grow operands - This grows the operand list in response
3377/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003378///
Jay Foade98f29d2011-04-01 08:00:58 +00003379void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003380 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003381 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003382
3383 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003384 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003385}
3386
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003387IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3388 Instruction *InsertBefore)
3389: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003390 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003391 init(Address, NumCases);
3392}
3393
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003394IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3395 BasicBlock *InsertAtEnd)
3396: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003397 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003398 init(Address, NumCases);
3399}
3400
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003401IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003402 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3403 nullptr, IBI.getNumOperands()) {
3404 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003405 Use *OL = getOperandList();
3406 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003407 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3408 OL[i] = InOL[i];
3409 SubclassOptionalData = IBI.SubclassOptionalData;
3410}
3411
Chris Lattner3ed871f2009-10-27 19:13:16 +00003412/// addDestination - Add a destination.
3413///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003414void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003415 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003416 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003417 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003418 // Initialize some new operands.
3419 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003420 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003421 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003422}
3423
3424/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003425/// indirectbr instruction.
3426void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003427 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3428
3429 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003430 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003431
3432 // Replace this value with the last one.
3433 OL[idx+1] = OL[NumOps-1];
3434
3435 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003436 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003437 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003438}
3439
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003440BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003441 return getSuccessor(idx);
3442}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003443unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003444 return getNumSuccessors();
3445}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003446void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003447 setSuccessor(idx, B);
3448}
3449
3450//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003451// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003452//===----------------------------------------------------------------------===//
3453
Chris Lattnerf22be932004-10-15 23:52:53 +00003454// Define these methods here so vtables don't get emitted into every translation
3455// unit that uses these classes.
3456
Devang Patel11cf3f42009-10-27 22:16:29 +00003457GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3458 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003459}
3460
Devang Patel11cf3f42009-10-27 22:16:29 +00003461BinaryOperator *BinaryOperator::clone_impl() const {
3462 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003463}
3464
Devang Patel11cf3f42009-10-27 22:16:29 +00003465FCmpInst* FCmpInst::clone_impl() const {
3466 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003467}
3468
Devang Patel11cf3f42009-10-27 22:16:29 +00003469ICmpInst* ICmpInst::clone_impl() const {
3470 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003471}
3472
Devang Patel11cf3f42009-10-27 22:16:29 +00003473ExtractValueInst *ExtractValueInst::clone_impl() const {
3474 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003475}
3476
Devang Patel11cf3f42009-10-27 22:16:29 +00003477InsertValueInst *InsertValueInst::clone_impl() const {
3478 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003479}
3480
Devang Patel11cf3f42009-10-27 22:16:29 +00003481AllocaInst *AllocaInst::clone_impl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003482 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3483 (Value *)getOperand(0), getAlignment());
3484 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3485 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003486}
3487
Devang Patel11cf3f42009-10-27 22:16:29 +00003488LoadInst *LoadInst::clone_impl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003489 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3490 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003491}
3492
Devang Patel11cf3f42009-10-27 22:16:29 +00003493StoreInst *StoreInst::clone_impl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003494 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003495 getAlignment(), getOrdering(), getSynchScope());
3496
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003497}
3498
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003499AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3500 AtomicCmpXchgInst *Result =
3501 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003502 getSuccessOrdering(), getFailureOrdering(),
3503 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003504 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003505 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003506 return Result;
3507}
3508
3509AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3510 AtomicRMWInst *Result =
3511 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3512 getOrdering(), getSynchScope());
3513 Result->setVolatile(isVolatile());
3514 return Result;
3515}
3516
Eli Friedmanfee02c62011-07-25 23:16:38 +00003517FenceInst *FenceInst::clone_impl() const {
3518 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3519}
3520
Devang Patel11cf3f42009-10-27 22:16:29 +00003521TruncInst *TruncInst::clone_impl() const {
3522 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003523}
3524
Devang Patel11cf3f42009-10-27 22:16:29 +00003525ZExtInst *ZExtInst::clone_impl() const {
3526 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003527}
3528
Devang Patel11cf3f42009-10-27 22:16:29 +00003529SExtInst *SExtInst::clone_impl() const {
3530 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003531}
3532
Devang Patel11cf3f42009-10-27 22:16:29 +00003533FPTruncInst *FPTruncInst::clone_impl() const {
3534 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003535}
3536
Devang Patel11cf3f42009-10-27 22:16:29 +00003537FPExtInst *FPExtInst::clone_impl() const {
3538 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003539}
3540
Devang Patel11cf3f42009-10-27 22:16:29 +00003541UIToFPInst *UIToFPInst::clone_impl() const {
3542 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003543}
3544
Devang Patel11cf3f42009-10-27 22:16:29 +00003545SIToFPInst *SIToFPInst::clone_impl() const {
3546 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003547}
3548
Devang Patel11cf3f42009-10-27 22:16:29 +00003549FPToUIInst *FPToUIInst::clone_impl() const {
3550 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003551}
3552
Devang Patel11cf3f42009-10-27 22:16:29 +00003553FPToSIInst *FPToSIInst::clone_impl() const {
3554 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003555}
3556
Devang Patel11cf3f42009-10-27 22:16:29 +00003557PtrToIntInst *PtrToIntInst::clone_impl() const {
3558 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003559}
3560
Devang Patel11cf3f42009-10-27 22:16:29 +00003561IntToPtrInst *IntToPtrInst::clone_impl() const {
3562 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003563}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003564
Devang Patel11cf3f42009-10-27 22:16:29 +00003565BitCastInst *BitCastInst::clone_impl() const {
3566 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003567}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003568
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003569AddrSpaceCastInst *AddrSpaceCastInst::clone_impl() const {
3570 return new AddrSpaceCastInst(getOperand(0), getType());
3571}
3572
Devang Patel11cf3f42009-10-27 22:16:29 +00003573CallInst *CallInst::clone_impl() const {
3574 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003575}
3576
Devang Patel11cf3f42009-10-27 22:16:29 +00003577SelectInst *SelectInst::clone_impl() const {
3578 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003579}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003580
Devang Patel11cf3f42009-10-27 22:16:29 +00003581VAArgInst *VAArgInst::clone_impl() const {
3582 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003583}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003584
Devang Patel11cf3f42009-10-27 22:16:29 +00003585ExtractElementInst *ExtractElementInst::clone_impl() const {
3586 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003587}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003588
Devang Patel11cf3f42009-10-27 22:16:29 +00003589InsertElementInst *InsertElementInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003590 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003591}
3592
Devang Patel11cf3f42009-10-27 22:16:29 +00003593ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003594 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003595}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003596
Devang Patel11cf3f42009-10-27 22:16:29 +00003597PHINode *PHINode::clone_impl() const {
3598 return new PHINode(*this);
3599}
3600
Bill Wendlingfae14752011-08-12 20:24:12 +00003601LandingPadInst *LandingPadInst::clone_impl() const {
3602 return new LandingPadInst(*this);
3603}
3604
Devang Patel11cf3f42009-10-27 22:16:29 +00003605ReturnInst *ReturnInst::clone_impl() const {
3606 return new(getNumOperands()) ReturnInst(*this);
3607}
3608
3609BranchInst *BranchInst::clone_impl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003610 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003611}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003612
Devang Patel11cf3f42009-10-27 22:16:29 +00003613SwitchInst *SwitchInst::clone_impl() const {
3614 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003615}
3616
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003617IndirectBrInst *IndirectBrInst::clone_impl() const {
3618 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003619}
3620
3621
Devang Patel11cf3f42009-10-27 22:16:29 +00003622InvokeInst *InvokeInst::clone_impl() const {
3623 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003624}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003625
Bill Wendlingf891bf82011-07-31 06:30:59 +00003626ResumeInst *ResumeInst::clone_impl() const {
3627 return new(1) ResumeInst(*this);
3628}
3629
Devang Patel11cf3f42009-10-27 22:16:29 +00003630UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003631 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003632 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003633}