blob: 02645511efc999d41ef14cfea69816e3e3cb6c69 [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";
David Majnemerb611e3f2015-08-14 05:09:07 +000065
66 if (Op1->getType()->isTokenTy())
67 return "select values cannot have token type";
68
Chris Lattner229907c2011-07-18 04:54:35 +000069 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000070 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000071 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000072 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000073 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000074 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000075 return "selected values for vector select must be vectors";
76 if (ET->getNumElements() != VT->getNumElements())
77 return "vector select requires selected vectors to have "
78 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000079 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000080 return "select condition must be i1 or <n x i1>";
81 }
Craig Topperc6207612014-04-09 06:08:46 +000082 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000083}
84
85
86//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000087// PHINode Class
88//===----------------------------------------------------------------------===//
89
Pete Cooper1d078692015-12-14 20:29:16 +000090void PHINode::anchor() {}
91
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000093 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
94 ReservedSpace(PN.getNumOperands()) {
95 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000096 std::copy(PN.op_begin(), PN.op_end(), op_begin());
97 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000098 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099}
100
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101// removeIncomingValue - Remove an incoming value. This is useful if a
102// predecessor basic block is deleted.
103Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000104 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105
106 // Move everything after this operand down.
107 //
108 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000110 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
112 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000113
114 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000115 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000116 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117
118 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000121 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 eraseFromParent();
123 }
124 return Removed;
125}
126
Jay Foade98f29d2011-04-01 08:00:58 +0000127/// growOperands - grow operands - This grows the operand list in response
128/// to a push_back style of operation. This grows the number of ops by 1.5
129/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130///
Jay Foade98f29d2011-04-01 08:00:58 +0000131void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000132 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000133 unsigned NumOps = e + e / 2;
134 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
135
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000137 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Nate Begemanb3923212005-08-04 23:24:19 +0000140/// hasConstantValue - If the specified PHI node always merges together the same
141/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000142Value *PHINode::hasConstantValue() const {
143 // Exploit the fact that phi nodes always have at least one entry.
144 Value *ConstantValue = getIncomingValue(0);
145 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000146 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
147 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000148 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000149 // The case where the first value is this PHI.
150 ConstantValue = getIncomingValue(i);
151 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000152 if (ConstantValue == this)
153 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000154 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000155}
156
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000157/// hasConstantOrUndefValue - Whether the specified PHI node always merges
158/// together the same value, assuming that undefs result in the same value as
159/// non-undefs.
160/// Unlike \ref hasConstantValue, this does not return a value because the
161/// unique non-undef incoming value need not dominate the PHI node.
162bool PHINode::hasConstantOrUndefValue() const {
163 Value *ConstantValue = nullptr;
164 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
165 Value *Incoming = getIncomingValue(i);
166 if (Incoming != this && !isa<UndefValue>(Incoming)) {
167 if (ConstantValue && ConstantValue != Incoming)
168 return false;
169 ConstantValue = Incoming;
170 }
171 }
172 return true;
173}
174
Bill Wendlingfae14752011-08-12 20:24:12 +0000175//===----------------------------------------------------------------------===//
176// LandingPadInst Implementation
177//===----------------------------------------------------------------------===//
178
David Majnemer7fddecc2015-06-17 20:52:32 +0000179LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
180 const Twine &NameStr, Instruction *InsertBefore)
181 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
182 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000183}
184
David Majnemer7fddecc2015-06-17 20:52:32 +0000185LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
186 const Twine &NameStr, BasicBlock *InsertAtEnd)
187 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
188 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000189}
190
191LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000192 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
193 LP.getNumOperands()),
194 ReservedSpace(LP.getNumOperands()) {
195 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000196 Use *OL = getOperandList();
197 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000198 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
199 OL[I] = InOL[I];
200
201 setCleanup(LP.isCleanup());
202}
203
David Majnemer7fddecc2015-06-17 20:52:32 +0000204LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000205 const Twine &NameStr,
206 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000207 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000208}
209
David Majnemer7fddecc2015-06-17 20:52:32 +0000210LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000211 const Twine &NameStr,
212 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000213 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000214}
215
David Majnemer7fddecc2015-06-17 20:52:32 +0000216void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000217 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000218 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000219 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000220 setName(NameStr);
221 setCleanup(false);
222}
223
224/// growOperands - grow operands - This grows the operand list in response to a
225/// push_back style of operation. This grows the number of ops by 2 times.
226void LandingPadInst::growOperands(unsigned Size) {
227 unsigned e = getNumOperands();
228 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000229 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000230 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000231}
232
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000233void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000234 unsigned OpNo = getNumOperands();
235 growOperands(1);
236 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000237 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000238 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000239}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240
241//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242// CallInst Implementation
243//===----------------------------------------------------------------------===//
244
Gordon Henriksen14a55692007-12-10 02:14:30 +0000245CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246}
247
David Blaikie348de692015-04-23 21:36:23 +0000248void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000249 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000250 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000251 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
252 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000253 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254
Jay Foad5bd375a2011-07-15 08:37:34 +0000255#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000256 assert((Args.size() == FTy->getNumParams() ||
257 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000258 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000259
260 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000261 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000262 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000263 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000264#endif
265
266 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000267
268 auto It = populateBundleOperandInfos(Bundles, Args.size());
269 (void)It;
270 assert(It + 1 == op_end() && "Should add up!");
271
Jay Foad5bd375a2011-07-15 08:37:34 +0000272 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273}
274
Jay Foad5bd375a2011-07-15 08:37:34 +0000275void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000276 FTy =
277 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000278 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000279 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000280
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000281 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000282
283 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284}
285
Daniel Dunbar4975db62009-07-25 04:41:11 +0000286CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 Instruction *InsertBefore)
288 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
289 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000290 Instruction::Call,
291 OperandTraits<CallInst>::op_end(this) - 1,
292 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000293 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294}
295
Daniel Dunbar4975db62009-07-25 04:41:11 +0000296CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297 BasicBlock *InsertAtEnd)
298 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
299 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000300 Instruction::Call,
301 OperandTraits<CallInst>::op_end(this) - 1,
302 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000303 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304}
305
Misha Brukmanb1c93172005-04-21 23:48:37 +0000306CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000307 : Instruction(CI.getType(), Instruction::Call,
308 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
309 CI.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000310 Attrs(CI.Attrs), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000311 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000312 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000313
Jay Foad5bd375a2011-07-15 08:37:34 +0000314 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000315 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
316 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000317 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
Sanjoy Das2d161452015-11-18 06:23:38 +0000320CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
321 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000322 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000323
324 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
325 InsertPt);
326 NewCI->setTailCallKind(CI->getTailCallKind());
327 NewCI->setCallingConv(CI->getCallingConv());
328 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000329 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000330 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000331 return NewCI;
332}
333
Hal Finkele87ad542016-07-10 23:01:32 +0000334Value *CallInst::getReturnedArgOperand() const {
335 unsigned Index;
336
Reid Klecknerb5180542017-03-21 16:57:19 +0000337 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Hal Finkele87ad542016-07-10 23:01:32 +0000338 return getArgOperand(Index-1);
339 if (const Function *F = getCalledFunction())
340 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
341 Index)
342 return getArgOperand(Index-1);
343
344 return nullptr;
345}
346
Amaury Sechet392638d2016-06-14 20:27:35 +0000347void CallInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000348 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000349 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000350 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000351}
352
Amaury Secheta65a2372016-06-15 05:14:29 +0000353void CallInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000354 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000355 PAL = PAL.addAttribute(getContext(), i, Attr);
356 setAttributes(PAL);
357}
358
Amaury Sechet392638d2016-06-14 20:27:35 +0000359void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000360 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000361 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000362 setAttributes(PAL);
363}
364
Amaury Sechet6100adf2016-06-15 17:50:39 +0000365void CallInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000366 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000367 PAL = PAL.removeAttribute(getContext(), i, Kind);
368 setAttributes(PAL);
369}
370
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000371void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000372 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000373 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
374 setAttributes(PAL);
375}
376
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000377void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000378 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000379 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
380 setAttributes(PAL);
381}
382
Amaury Sechet392638d2016-06-14 20:27:35 +0000383bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000384 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
385
Reid Klecknerb5180542017-03-21 16:57:19 +0000386 if (Attrs.hasAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000387 return true;
388 if (const Function *F = getCalledFunction())
Amaury Sechet392638d2016-06-14 20:27:35 +0000389 return F->getAttributes().hasAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000390 return false;
391}
392
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000393bool CallInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000394 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000395 // There are getNumOperands() - 1 data operands. The last operand is the
396 // callee.
397 assert(i < getNumOperands() && "Data operand index out of bounds!");
398
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000399 // The attribute A can either be directly specified, if the operand in
400 // question is a call argument; or be indirectly implied by the kind of its
401 // containing operand bundle, if the operand is a bundle operand.
402
403 if (i < (getNumArgOperands() + 1))
Amaury Sechet392638d2016-06-14 20:27:35 +0000404 return paramHasAttr(i, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000405
406 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
407 "Must be either a call argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000408 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000409}
410
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000411/// IsConstantOne - Return true only if val is constant int 1
412static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000413 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000414 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
415 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000416}
417
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000418static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000419 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000420 Type *AllocTy, Value *AllocSize,
421 Value *ArraySize,
422 ArrayRef<OperandBundleDef> OpB,
423 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000424 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000425 "createMalloc needs either InsertBefore or InsertAtEnd");
426
427 // malloc(type) becomes:
428 // bitcast (i8* malloc(typeSize)) to type*
429 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000430 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000431 if (!ArraySize)
432 ArraySize = ConstantInt::get(IntPtrTy, 1);
433 else if (ArraySize->getType() != IntPtrTy) {
434 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000435 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
436 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000437 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000438 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
439 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000440 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000441
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000442 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000443 if (IsConstantOne(AllocSize)) {
444 AllocSize = ArraySize; // Operand * 1 = Operand
445 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
446 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
447 false /*ZExt*/);
448 // Malloc arg is constant product of type size and array size
449 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
450 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000451 // Multiply type size by the array size...
452 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000453 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
454 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000455 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000456 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
457 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000458 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000459 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000460
Victor Hernandez788eaab2009-09-18 19:20:02 +0000461 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000462 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000463 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
464 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000465 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000466 Value *MallocFunc = MallocF;
467 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000468 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000469 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000470 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000471 CallInst *MCall = nullptr;
472 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000473 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000474 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
475 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000476 Result = MCall;
477 if (Result->getType() != AllocPtrType)
478 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000479 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000480 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000481 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000482 Result = MCall;
483 if (Result->getType() != AllocPtrType) {
484 InsertAtEnd->getInstList().push_back(MCall);
485 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000486 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000487 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000488 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000489 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000490 if (Function *F = dyn_cast<Function>(MallocFunc)) {
491 MCall->setCallingConv(F->getCallingConv());
492 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
493 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000494 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000495
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000496 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000497}
498
499/// CreateMalloc - Generate the IR for a call to malloc:
500/// 1. Compute the malloc call's argument as the specified type's size,
501/// possibly multiplied by the array size if the array size is not
502/// constant 1.
503/// 2. Call malloc with that argument.
504/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000505Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000506 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000507 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000508 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000509 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000510 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000511 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000512}
David Majnemerfadc6db2016-04-29 08:07:22 +0000513Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
514 Type *IntPtrTy, Type *AllocTy,
515 Value *AllocSize, Value *ArraySize,
516 ArrayRef<OperandBundleDef> OpB,
517 Function *MallocF,
518 const Twine &Name) {
519 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
520 ArraySize, OpB, MallocF, Name);
521}
522
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000523
524/// CreateMalloc - Generate the IR for a call to malloc:
525/// 1. Compute the malloc call's argument as the specified type's size,
526/// possibly multiplied by the array size if the array size is not
527/// constant 1.
528/// 2. Call malloc with that argument.
529/// 3. Bitcast the result of the malloc call to the specified type.
530/// Note: This function does not add the bitcast to the basic block, that is the
531/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000532Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000533 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000534 Value *AllocSize, Value *ArraySize,
535 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000536 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000537 ArraySize, None, MallocF, Name);
538}
539Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
540 Type *IntPtrTy, Type *AllocTy,
541 Value *AllocSize, Value *ArraySize,
542 ArrayRef<OperandBundleDef> OpB,
543 Function *MallocF, const Twine &Name) {
544 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
545 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000546}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000547
David Majnemerfadc6db2016-04-29 08:07:22 +0000548static Instruction *createFree(Value *Source,
549 ArrayRef<OperandBundleDef> Bundles,
550 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000551 BasicBlock *InsertAtEnd) {
552 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
553 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000554 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000555 "Can not free something of nonpointer type!");
556
Ana Pazosb3596022016-02-03 21:34:39 +0000557 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
558 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000559
Chris Lattner229907c2011-07-18 04:54:35 +0000560 Type *VoidTy = Type::getVoidTy(M->getContext());
561 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000562 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000563 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Ana Pazosb3596022016-02-03 21:34:39 +0000564 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000565 Value *PtrCast = Source;
566 if (InsertBefore) {
567 if (Source->getType() != IntPtrTy)
568 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000569 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000570 } else {
571 if (Source->getType() != IntPtrTy)
572 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000573 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000574 }
575 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000576 if (Function *F = dyn_cast<Function>(FreeFunc))
577 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000578
579 return Result;
580}
581
582/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000583Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000584 return createFree(Source, None, InsertBefore, nullptr);
585}
586Instruction *CallInst::CreateFree(Value *Source,
587 ArrayRef<OperandBundleDef> Bundles,
588 Instruction *InsertBefore) {
589 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000590}
591
592/// CreateFree - Generate the IR for a call to the builtin free function.
593/// Note: This function does not add the call to the basic block, that is the
594/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000595Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000596 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
597 assert(FreeCall && "CreateFree did not create a CallInst");
598 return FreeCall;
599}
600Instruction *CallInst::CreateFree(Value *Source,
601 ArrayRef<OperandBundleDef> Bundles,
602 BasicBlock *InsertAtEnd) {
603 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000604 assert(FreeCall && "CreateFree did not create a CallInst");
605 return FreeCall;
606}
607
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000608//===----------------------------------------------------------------------===//
609// InvokeInst Implementation
610//===----------------------------------------------------------------------===//
611
David Blaikie3e807092015-05-13 18:35:26 +0000612void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
613 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000614 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000615 const Twine &NameStr) {
616 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000617
Sanjoy Das9303c242015-09-24 19:14:18 +0000618 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
619 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000620 Op<-3>() = Fn;
621 Op<-2>() = IfNormal;
622 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000623
624#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000625 assert(((Args.size() == FTy->getNumParams()) ||
626 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000627 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000628
Jay Foad5bd375a2011-07-15 08:37:34 +0000629 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000630 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000631 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000632 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000633#endif
634
635 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000636
637 auto It = populateBundleOperandInfos(Bundles, Args.size());
638 (void)It;
639 assert(It + 3 == op_end() && "Should add up!");
640
Jay Foad5bd375a2011-07-15 08:37:34 +0000641 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000642}
643
Misha Brukmanb1c93172005-04-21 23:48:37 +0000644InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000645 : TerminatorInst(II.getType(), Instruction::Invoke,
646 OperandTraits<InvokeInst>::op_end(this) -
647 II.getNumOperands(),
648 II.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000649 Attrs(II.Attrs), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000650 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000651 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000652 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
653 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000654 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000655}
656
Sanjoy Das2d161452015-11-18 06:23:38 +0000657InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
658 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000659 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000660
661 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
662 II->getUnwindDest(), Args, OpB,
663 II->getName(), InsertPt);
664 NewII->setCallingConv(II->getCallingConv());
665 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000666 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000667 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000668 return NewII;
669}
670
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000671BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
672 return getSuccessor(idx);
673}
674unsigned InvokeInst::getNumSuccessorsV() const {
675 return getNumSuccessors();
676}
677void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
678 return setSuccessor(idx, B);
679}
680
Hal Finkele87ad542016-07-10 23:01:32 +0000681Value *InvokeInst::getReturnedArgOperand() const {
682 unsigned Index;
683
Reid Klecknerb5180542017-03-21 16:57:19 +0000684 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Hal Finkele87ad542016-07-10 23:01:32 +0000685 return getArgOperand(Index-1);
686 if (const Function *F = getCalledFunction())
687 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
688 Index)
689 return getArgOperand(Index-1);
690
691 return nullptr;
692}
693
Amaury Sechet392638d2016-06-14 20:27:35 +0000694bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000695 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
696
Reid Klecknerb5180542017-03-21 16:57:19 +0000697 if (Attrs.hasAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000698 return true;
699 if (const Function *F = getCalledFunction())
Amaury Sechet392638d2016-06-14 20:27:35 +0000700 return F->getAttributes().hasAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000701 return false;
702}
703
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000704bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000705 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000706 // There are getNumOperands() - 3 data operands. The last three operands are
707 // the callee and the two successor basic blocks.
708 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
709
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000710 // The attribute A can either be directly specified, if the operand in
711 // question is an invoke argument; or be indirectly implied by the kind of its
712 // containing operand bundle, if the operand is a bundle operand.
713
714 if (i < (getNumArgOperands() + 1))
Amaury Sechet392638d2016-06-14 20:27:35 +0000715 return paramHasAttr(i, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000716
717 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
718 "Must be either an invoke argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000719 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000720}
721
Amaury Sechet392638d2016-06-14 20:27:35 +0000722void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000723 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000724 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000725 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000726}
727
Amaury Secheta65a2372016-06-15 05:14:29 +0000728void InvokeInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000729 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000730 PAL = PAL.addAttribute(getContext(), i, Attr);
731 setAttributes(PAL);
732}
733
Amaury Sechet392638d2016-06-14 20:27:35 +0000734void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000735 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000736 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000737 setAttributes(PAL);
738}
739
Amaury Sechet6100adf2016-06-15 17:50:39 +0000740void InvokeInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000741 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000742 PAL = PAL.removeAttribute(getContext(), i, Kind);
743 setAttributes(PAL);
744}
745
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000746void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000747 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000748 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
749 setAttributes(PAL);
750}
751
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000752void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000753 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000754 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
755 setAttributes(PAL);
756}
757
Bill Wendlingfae14752011-08-12 20:24:12 +0000758LandingPadInst *InvokeInst::getLandingPadInst() const {
759 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
760}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000761
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000762//===----------------------------------------------------------------------===//
763// ReturnInst Implementation
764//===----------------------------------------------------------------------===//
765
Chris Lattner2195fc42007-02-24 00:55:48 +0000766ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000767 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000768 OperandTraits<ReturnInst>::op_end(this) -
769 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000770 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000771 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000772 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000773 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000774}
775
Owen Anderson55f1c092009-08-13 21:58:54 +0000776ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
777 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000778 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
779 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000780 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000781 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000782}
Owen Anderson55f1c092009-08-13 21:58:54 +0000783ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
784 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000785 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
786 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000787 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000788 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000789}
Owen Anderson55f1c092009-08-13 21:58:54 +0000790ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
791 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000792 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000793}
794
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000795unsigned ReturnInst::getNumSuccessorsV() const {
796 return getNumSuccessors();
797}
798
Devang Patelae682fb2008-02-26 17:56:20 +0000799/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
800/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000801void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000802 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803}
804
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000805BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000806 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000807}
808
Devang Patel59643e52008-02-23 00:35:18 +0000809ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000810}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000811
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000813// ResumeInst Implementation
814//===----------------------------------------------------------------------===//
815
816ResumeInst::ResumeInst(const ResumeInst &RI)
817 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
818 OperandTraits<ResumeInst>::op_begin(this), 1) {
819 Op<0>() = RI.Op<0>();
820}
821
822ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
823 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
824 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
825 Op<0>() = Exn;
826}
827
828ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
829 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
830 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
831 Op<0>() = Exn;
832}
833
834unsigned ResumeInst::getNumSuccessorsV() const {
835 return getNumSuccessors();
836}
837
838void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
839 llvm_unreachable("ResumeInst has no successors!");
840}
841
842BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
843 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000844}
845
846//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000847// CleanupReturnInst Implementation
848//===----------------------------------------------------------------------===//
849
850CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
851 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
852 OperandTraits<CleanupReturnInst>::op_end(this) -
853 CRI.getNumOperands(),
854 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000855 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000856 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000857 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000858 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000859}
860
David Majnemer8a1c45d2015-12-12 05:38:55 +0000861void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000862 if (UnwindBB)
863 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000864
David Majnemer8a1c45d2015-12-12 05:38:55 +0000865 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000866 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000867 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000868}
869
David Majnemer8a1c45d2015-12-12 05:38:55 +0000870CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
871 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000872 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
873 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000874 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
875 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000876 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000877}
878
David Majnemer8a1c45d2015-12-12 05:38:55 +0000879CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
880 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000881 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
882 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000883 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
884 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000885 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000886}
887
888BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
889 assert(Idx == 0);
890 return getUnwindDest();
891}
892unsigned CleanupReturnInst::getNumSuccessorsV() const {
893 return getNumSuccessors();
894}
895void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
896 assert(Idx == 0);
897 setUnwindDest(B);
898}
899
900//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000901// CatchReturnInst Implementation
902//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000903void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000904 Op<0>() = CatchPad;
905 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000906}
David Majnemer654e1302015-07-31 17:58:14 +0000907
908CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
909 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000910 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
911 Op<0>() = CRI.Op<0>();
912 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000913}
914
David Majnemer8a1c45d2015-12-12 05:38:55 +0000915CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000916 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000917 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000918 OperandTraits<CatchReturnInst>::op_begin(this), 2,
919 InsertBefore) {
920 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000921}
922
David Majnemer8a1c45d2015-12-12 05:38:55 +0000923CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000924 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000925 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000926 OperandTraits<CatchReturnInst>::op_begin(this), 2,
927 InsertAtEnd) {
928 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000929}
930
931BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000932 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000933 return getSuccessor();
934}
935unsigned CatchReturnInst::getNumSuccessorsV() const {
936 return getNumSuccessors();
937}
938void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000939 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000940 setSuccessor(B);
941}
942
943//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000944// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000945//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000946
947CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
948 unsigned NumReservedValues,
949 const Twine &NameStr,
950 Instruction *InsertBefore)
951 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
952 InsertBefore) {
953 if (UnwindDest)
954 ++NumReservedValues;
955 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000956 setName(NameStr);
957}
958
David Majnemer8a1c45d2015-12-12 05:38:55 +0000959CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
960 unsigned NumReservedValues,
961 const Twine &NameStr, BasicBlock *InsertAtEnd)
962 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000963 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000964 if (UnwindDest)
965 ++NumReservedValues;
966 init(ParentPad, UnwindDest, NumReservedValues + 1);
967 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000968}
969
David Majnemer8a1c45d2015-12-12 05:38:55 +0000970CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
971 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
972 CSI.getNumOperands()) {
973 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
974 setNumHungOffUseOperands(ReservedSpace);
975 Use *OL = getOperandList();
976 const Use *InOL = CSI.getOperandList();
977 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
978 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000979}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000980
981void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
982 unsigned NumReservedValues) {
983 assert(ParentPad && NumReservedValues);
984
985 ReservedSpace = NumReservedValues;
986 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
987 allocHungoffUses(ReservedSpace);
988
989 Op<0>() = ParentPad;
990 if (UnwindDest) {
991 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
992 setUnwindDest(UnwindDest);
993 }
994}
995
996/// growOperands - grow operands - This grows the operand list in response to a
997/// push_back style of operation. This grows the number of ops by 2 times.
998void CatchSwitchInst::growOperands(unsigned Size) {
999 unsigned NumOperands = getNumOperands();
1000 assert(NumOperands >= 1);
1001 if (ReservedSpace >= NumOperands + Size)
1002 return;
1003 ReservedSpace = (NumOperands + Size / 2) * 2;
1004 growHungoffUses(ReservedSpace);
1005}
1006
1007void CatchSwitchInst::addHandler(BasicBlock *Handler) {
1008 unsigned OpNo = getNumOperands();
1009 growOperands(1);
1010 assert(OpNo < ReservedSpace && "Growing didn't work!");
1011 setNumHungOffUseOperands(getNumOperands() + 1);
1012 getOperandList()[OpNo] = Handler;
1013}
1014
Joseph Tremoulet0d808882016-01-05 02:37:41 +00001015void CatchSwitchInst::removeHandler(handler_iterator HI) {
1016 // Move all subsequent handlers up one.
1017 Use *EndDst = op_end() - 1;
1018 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
1019 *CurDst = *(CurDst + 1);
1020 // Null out the last handler use.
1021 *EndDst = nullptr;
1022
1023 setNumHungOffUseOperands(getNumOperands() - 1);
1024}
1025
David Majnemer8a1c45d2015-12-12 05:38:55 +00001026BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
1027 return getSuccessor(idx);
1028}
1029unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +00001030 return getNumSuccessors();
1031}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001032void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1033 setSuccessor(idx, B);
1034}
1035
1036//===----------------------------------------------------------------------===//
1037// FuncletPadInst Implementation
1038//===----------------------------------------------------------------------===//
1039void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1040 const Twine &NameStr) {
1041 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
1042 std::copy(Args.begin(), Args.end(), op_begin());
1043 setParentPad(ParentPad);
1044 setName(NameStr);
1045}
1046
1047FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
1048 : Instruction(FPI.getType(), FPI.getOpcode(),
1049 OperandTraits<FuncletPadInst>::op_end(this) -
1050 FPI.getNumOperands(),
1051 FPI.getNumOperands()) {
1052 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1053 setParentPad(FPI.getParentPad());
1054}
1055
1056FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1057 ArrayRef<Value *> Args, unsigned Values,
1058 const Twine &NameStr, Instruction *InsertBefore)
1059 : Instruction(ParentPad->getType(), Op,
1060 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1061 InsertBefore) {
1062 init(ParentPad, Args, NameStr);
1063}
1064
1065FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1066 ArrayRef<Value *> Args, unsigned Values,
1067 const Twine &NameStr, BasicBlock *InsertAtEnd)
1068 : Instruction(ParentPad->getType(), Op,
1069 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1070 InsertAtEnd) {
1071 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001072}
1073
1074//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001075// UnreachableInst Implementation
1076//===----------------------------------------------------------------------===//
1077
Owen Anderson55f1c092009-08-13 21:58:54 +00001078UnreachableInst::UnreachableInst(LLVMContext &Context,
1079 Instruction *InsertBefore)
1080 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001081 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001082}
Owen Anderson55f1c092009-08-13 21:58:54 +00001083UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1084 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001085 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001086}
1087
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001088unsigned UnreachableInst::getNumSuccessorsV() const {
1089 return getNumSuccessors();
1090}
1091
1092void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001093 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001094}
1095
1096BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001097 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001098}
1099
1100//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001101// BranchInst Implementation
1102//===----------------------------------------------------------------------===//
1103
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001104void BranchInst::AssertOK() {
1105 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001106 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001107 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001108}
1109
Chris Lattner2195fc42007-02-24 00:55:48 +00001110BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001111 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001112 OperandTraits<BranchInst>::op_end(this) - 1,
1113 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001114 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001115 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001116}
1117BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1118 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001119 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001120 OperandTraits<BranchInst>::op_end(this) - 3,
1121 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001122 Op<-1>() = IfTrue;
1123 Op<-2>() = IfFalse;
1124 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001125#ifndef NDEBUG
1126 AssertOK();
1127#endif
1128}
1129
1130BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001131 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001132 OperandTraits<BranchInst>::op_end(this) - 1,
1133 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001134 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001135 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001136}
1137
1138BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1139 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001140 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001141 OperandTraits<BranchInst>::op_end(this) - 3,
1142 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001143 Op<-1>() = IfTrue;
1144 Op<-2>() = IfFalse;
1145 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001146#ifndef NDEBUG
1147 AssertOK();
1148#endif
1149}
1150
1151
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001152BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001153 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001154 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1155 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001156 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001157 if (BI.getNumOperands() != 1) {
1158 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001159 Op<-3>() = BI.Op<-3>();
1160 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001161 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001162 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001163}
1164
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001165void BranchInst::swapSuccessors() {
1166 assert(isConditional() &&
1167 "Cannot swap successors of an unconditional branch");
1168 Op<-1>().swap(Op<-2>());
1169
1170 // Update profile metadata if present and it matches our structural
1171 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +00001172 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001173}
1174
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001175BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1176 return getSuccessor(idx);
1177}
1178unsigned BranchInst::getNumSuccessorsV() const {
1179 return getNumSuccessors();
1180}
1181void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1182 setSuccessor(idx, B);
1183}
1184
1185
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001186//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001187// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001188//===----------------------------------------------------------------------===//
1189
Owen Andersonb6b25302009-07-14 23:09:55 +00001190static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001191 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001192 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001193 else {
1194 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001195 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001196 assert(Amt->getType()->isIntegerTy() &&
1197 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001198 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001199 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001200}
1201
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001202AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1203 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001204
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001205AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1206 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001207
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001208AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001209 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001210 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001211
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001212AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001213 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001214 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001215
Chris Lattner229907c2011-07-18 04:54:35 +00001216AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001217 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001218 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1219 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1220 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001221 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001222 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001223 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001224}
1225
Chris Lattner229907c2011-07-18 04:54:35 +00001226AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001227 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001228 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1229 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1230 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001231 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001232 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001233 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001234}
1235
Gordon Henriksen14a55692007-12-10 02:14:30 +00001236// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001237AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001238}
1239
Victor Hernandez8acf2952009-10-23 21:09:37 +00001240void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001241 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001242 assert(Align <= MaximumAlignment &&
1243 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001244 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1245 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001246 assert(getAlignment() == Align && "Alignment representation error!");
1247}
1248
Victor Hernandez8acf2952009-10-23 21:09:37 +00001249bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001250 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001251 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001252 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001253}
1254
Chris Lattner8b291e62008-11-26 02:54:17 +00001255/// isStaticAlloca - Return true if this alloca is in the entry block of the
1256/// function and is a constant size. If so, the code generator will fold it
1257/// into the prolog/epilog code, so it is basically free.
1258bool AllocaInst::isStaticAlloca() const {
1259 // Must be constant size.
1260 if (!isa<ConstantInt>(getArraySize())) return false;
1261
1262 // Must be in the entry block.
1263 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001264 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001265}
1266
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001267//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001268// LoadInst Implementation
1269//===----------------------------------------------------------------------===//
1270
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001271void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001272 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001273 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001274 assert(!(isAtomic() && getAlignment() == 0) &&
1275 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001276}
1277
Daniel Dunbar4975db62009-07-25 04:41:11 +00001278LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001279 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001280
Daniel Dunbar4975db62009-07-25 04:41:11 +00001281LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001282 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001283
David Blaikie0c28fd72015-05-20 21:46:30 +00001284LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001285 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001286 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001287
1288LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1289 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001290 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001291
David Blaikieb7a029872015-04-17 19:56:21 +00001292LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001293 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001294 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1295 CrossThread, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001296
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001297LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001298 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001299 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1300 CrossThread, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001301
David Blaikie15d9a4c2015-04-06 20:59:48 +00001302LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001303 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001304 SynchronizationScope SynchScope, Instruction *InsertBef)
1305 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001306 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001307 setVolatile(isVolatile);
1308 setAlignment(Align);
1309 setAtomic(Order, SynchScope);
1310 AssertOK();
1311 setName(Name);
1312}
1313
1314LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1315 unsigned Align, AtomicOrdering Order,
1316 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001317 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001318 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001319 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001320 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001321 setAlignment(Align);
1322 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001323 AssertOK();
1324 setName(Name);
1325}
1326
Daniel Dunbar27096822009-08-11 18:11:15 +00001327LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1328 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1329 Load, Ptr, InsertBef) {
1330 setVolatile(false);
1331 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001332 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001333 AssertOK();
1334 if (Name && Name[0]) setName(Name);
1335}
1336
1337LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1338 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1339 Load, Ptr, InsertAE) {
1340 setVolatile(false);
1341 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001342 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001343 AssertOK();
1344 if (Name && Name[0]) setName(Name);
1345}
1346
David Blaikie0c28fd72015-05-20 21:46:30 +00001347LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001348 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001349 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1350 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001351 setVolatile(isVolatile);
1352 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001353 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001354 AssertOK();
1355 if (Name && Name[0]) setName(Name);
1356}
1357
1358LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1359 BasicBlock *InsertAE)
1360 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1361 Load, Ptr, InsertAE) {
1362 setVolatile(isVolatile);
1363 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001364 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001365 AssertOK();
1366 if (Name && Name[0]) setName(Name);
1367}
1368
Christopher Lamb84485702007-04-22 19:24:39 +00001369void LoadInst::setAlignment(unsigned Align) {
1370 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001371 assert(Align <= MaximumAlignment &&
1372 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001373 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001374 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001375 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001376}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001377
1378//===----------------------------------------------------------------------===//
1379// StoreInst Implementation
1380//===----------------------------------------------------------------------===//
1381
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001382void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001383 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001384 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001385 "Ptr must have pointer type!");
1386 assert(getOperand(0)->getType() ==
1387 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001388 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001389 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001390 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001391}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001392
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001393StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001394 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001395
1396StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001397 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001398
Misha Brukmanb1c93172005-04-21 23:48:37 +00001399StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001400 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001401 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001402
1403StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001404 BasicBlock *InsertAtEnd)
1405 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1406
1407StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1408 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001409 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1410 CrossThread, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001411
1412StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1413 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001414 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1415 CrossThread, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001416
Misha Brukmanb1c93172005-04-21 23:48:37 +00001417StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001418 unsigned Align, AtomicOrdering Order,
1419 SynchronizationScope SynchScope,
1420 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001421 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001422 OperandTraits<StoreInst>::op_begin(this),
1423 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001424 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001425 Op<0>() = val;
1426 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001427 setVolatile(isVolatile);
1428 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001429 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001430 AssertOK();
1431}
1432
1433StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001434 unsigned Align, AtomicOrdering Order,
1435 SynchronizationScope SynchScope,
1436 BasicBlock *InsertAtEnd)
1437 : Instruction(Type::getVoidTy(val->getContext()), Store,
1438 OperandTraits<StoreInst>::op_begin(this),
1439 OperandTraits<StoreInst>::operands(this),
1440 InsertAtEnd) {
1441 Op<0>() = val;
1442 Op<1>() = addr;
1443 setVolatile(isVolatile);
1444 setAlignment(Align);
1445 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001446 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001447}
1448
Christopher Lamb84485702007-04-22 19:24:39 +00001449void StoreInst::setAlignment(unsigned Align) {
1450 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001451 assert(Align <= MaximumAlignment &&
1452 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001453 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001454 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001455 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001456}
1457
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001458//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001459// AtomicCmpXchgInst Implementation
1460//===----------------------------------------------------------------------===//
1461
1462void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001463 AtomicOrdering SuccessOrdering,
1464 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001465 SynchronizationScope SynchScope) {
1466 Op<0>() = Ptr;
1467 Op<1>() = Cmp;
1468 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001469 setSuccessOrdering(SuccessOrdering);
1470 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001471 setSynchScope(SynchScope);
1472
1473 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1474 "All operands must be non-null!");
1475 assert(getOperand(0)->getType()->isPointerTy() &&
1476 "Ptr must have pointer type!");
1477 assert(getOperand(1)->getType() ==
1478 cast<PointerType>(getOperand(0)->getType())->getElementType()
1479 && "Ptr must be a pointer to Cmp type!");
1480 assert(getOperand(2)->getType() ==
1481 cast<PointerType>(getOperand(0)->getType())->getElementType()
1482 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001483 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001484 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001485 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001486 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001487 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1488 "AtomicCmpXchg failure argument shall be no stronger than the success "
1489 "argument");
1490 assert(FailureOrdering != AtomicOrdering::Release &&
1491 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001492 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001493}
1494
1495AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001496 AtomicOrdering SuccessOrdering,
1497 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001498 SynchronizationScope SynchScope,
1499 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001500 : Instruction(
1501 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1502 nullptr),
1503 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1504 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001505 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001506}
1507
1508AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001509 AtomicOrdering SuccessOrdering,
1510 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001511 SynchronizationScope SynchScope,
1512 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001513 : Instruction(
1514 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1515 nullptr),
1516 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1517 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001518 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001519}
Tim Northover420a2162014-06-13 14:24:07 +00001520
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001521//===----------------------------------------------------------------------===//
1522// AtomicRMWInst Implementation
1523//===----------------------------------------------------------------------===//
1524
1525void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1526 AtomicOrdering Ordering,
1527 SynchronizationScope SynchScope) {
1528 Op<0>() = Ptr;
1529 Op<1>() = Val;
1530 setOperation(Operation);
1531 setOrdering(Ordering);
1532 setSynchScope(SynchScope);
1533
1534 assert(getOperand(0) && getOperand(1) &&
1535 "All operands must be non-null!");
1536 assert(getOperand(0)->getType()->isPointerTy() &&
1537 "Ptr must have pointer type!");
1538 assert(getOperand(1)->getType() ==
1539 cast<PointerType>(getOperand(0)->getType())->getElementType()
1540 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001541 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001542 "AtomicRMW instructions must be atomic!");
1543}
1544
1545AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1546 AtomicOrdering Ordering,
1547 SynchronizationScope SynchScope,
1548 Instruction *InsertBefore)
1549 : Instruction(Val->getType(), AtomicRMW,
1550 OperandTraits<AtomicRMWInst>::op_begin(this),
1551 OperandTraits<AtomicRMWInst>::operands(this),
1552 InsertBefore) {
1553 Init(Operation, Ptr, Val, Ordering, SynchScope);
1554}
1555
1556AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1557 AtomicOrdering Ordering,
1558 SynchronizationScope SynchScope,
1559 BasicBlock *InsertAtEnd)
1560 : Instruction(Val->getType(), AtomicRMW,
1561 OperandTraits<AtomicRMWInst>::op_begin(this),
1562 OperandTraits<AtomicRMWInst>::operands(this),
1563 InsertAtEnd) {
1564 Init(Operation, Ptr, Val, Ordering, SynchScope);
1565}
1566
1567//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001568// FenceInst Implementation
1569//===----------------------------------------------------------------------===//
1570
1571FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1572 SynchronizationScope SynchScope,
1573 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001574 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001575 setOrdering(Ordering);
1576 setSynchScope(SynchScope);
1577}
1578
1579FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1580 SynchronizationScope SynchScope,
1581 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001582 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001583 setOrdering(Ordering);
1584 setSynchScope(SynchScope);
1585}
1586
1587//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001588// GetElementPtrInst Implementation
1589//===----------------------------------------------------------------------===//
1590
Pete Cooper1d078692015-12-14 20:29:16 +00001591void GetElementPtrInst::anchor() {}
1592
Jay Foadd1b78492011-07-25 09:48:08 +00001593void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001594 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001595 assert(getNumOperands() == 1 + IdxList.size() &&
1596 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001597 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001598 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001599 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001600}
1601
Gabor Greiff6caff662008-05-10 08:32:32 +00001602GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001603 : Instruction(GEPI.getType(), GetElementPtr,
1604 OperandTraits<GetElementPtrInst>::op_end(this) -
1605 GEPI.getNumOperands(),
1606 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001607 SourceElementType(GEPI.SourceElementType),
1608 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001609 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001610 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001611}
1612
Chris Lattner6090a422009-03-09 04:46:40 +00001613/// getIndexedType - Returns the type of the element that would be accessed with
1614/// a gep instruction with the specified parameters.
1615///
1616/// The Idxs pointer should point to a continuous piece of memory containing the
1617/// indices, either as Value* or uint64_t.
1618///
1619/// A null type is returned if the indices are invalid for the specified
1620/// pointer type.
1621///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001622template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001623static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001624 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001625 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001626 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001627
Chris Lattner6090a422009-03-09 04:46:40 +00001628 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001629 // it cannot be 'stepped over'.
1630 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001631 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001632
Dan Gohman1ecaf452008-05-31 00:58:22 +00001633 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001634 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001635 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001636 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001637 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001638 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001639 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001640 }
Craig Topperc6207612014-04-09 06:08:46 +00001641 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001642}
1643
David Blaikied288fb82015-03-30 21:41:43 +00001644Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1645 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001646}
1647
David Blaikied288fb82015-03-30 21:41:43 +00001648Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001649 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001650 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001651}
1652
David Blaikied288fb82015-03-30 21:41:43 +00001653Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1654 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001655}
1656
Chris Lattner45f15572007-04-14 00:12:57 +00001657/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1658/// zeros. If so, the result pointer and the first operand have the same
1659/// value, just potentially different types.
1660bool GetElementPtrInst::hasAllZeroIndices() const {
1661 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1662 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1663 if (!CI->isZero()) return false;
1664 } else {
1665 return false;
1666 }
1667 }
1668 return true;
1669}
1670
Chris Lattner27058292007-04-27 20:35:56 +00001671/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1672/// constant integers. If so, the result pointer and the first operand have
1673/// a constant offset between them.
1674bool GetElementPtrInst::hasAllConstantIndices() const {
1675 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1676 if (!isa<ConstantInt>(getOperand(i)))
1677 return false;
1678 }
1679 return true;
1680}
1681
Dan Gohman1b849082009-09-07 23:54:19 +00001682void GetElementPtrInst::setIsInBounds(bool B) {
1683 cast<GEPOperator>(this)->setIsInBounds(B);
1684}
Chris Lattner45f15572007-04-14 00:12:57 +00001685
Nick Lewycky28a5f252009-09-27 21:33:04 +00001686bool GetElementPtrInst::isInBounds() const {
1687 return cast<GEPOperator>(this)->isInBounds();
1688}
1689
Chandler Carruth1e140532012-12-11 10:29:10 +00001690bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1691 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001692 // Delegate to the generic GEPOperator implementation.
1693 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001694}
1695
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001696//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001697// ExtractElementInst Implementation
1698//===----------------------------------------------------------------------===//
1699
1700ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001701 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001702 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001703 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001704 ExtractElement,
1705 OperandTraits<ExtractElementInst>::op_begin(this),
1706 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001707 assert(isValidOperands(Val, Index) &&
1708 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001709 Op<0>() = Val;
1710 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001711 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001712}
1713
1714ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001715 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001716 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001717 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001718 ExtractElement,
1719 OperandTraits<ExtractElementInst>::op_begin(this),
1720 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001721 assert(isValidOperands(Val, Index) &&
1722 "Invalid extractelement instruction operands!");
1723
Gabor Greif2d3024d2008-05-26 21:33:52 +00001724 Op<0>() = Val;
1725 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001726 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001727}
1728
Chris Lattner65511ff2006-10-05 06:24:58 +00001729
Chris Lattner54865b32006-04-08 04:05:48 +00001730bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001731 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001732 return false;
1733 return true;
1734}
1735
1736
Robert Bocchino23004482006-01-10 19:05:34 +00001737//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001738// InsertElementInst Implementation
1739//===----------------------------------------------------------------------===//
1740
Chris Lattner54865b32006-04-08 04:05:48 +00001741InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001742 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001743 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001744 : Instruction(Vec->getType(), InsertElement,
1745 OperandTraits<InsertElementInst>::op_begin(this),
1746 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001747 assert(isValidOperands(Vec, Elt, Index) &&
1748 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001749 Op<0>() = Vec;
1750 Op<1>() = Elt;
1751 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001752 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001753}
1754
Chris Lattner54865b32006-04-08 04:05:48 +00001755InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001756 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001757 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001758 : Instruction(Vec->getType(), InsertElement,
1759 OperandTraits<InsertElementInst>::op_begin(this),
1760 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001761 assert(isValidOperands(Vec, Elt, Index) &&
1762 "Invalid insertelement instruction operands!");
1763
Gabor Greif2d3024d2008-05-26 21:33:52 +00001764 Op<0>() = Vec;
1765 Op<1>() = Elt;
1766 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001767 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001768}
1769
Chris Lattner54865b32006-04-08 04:05:48 +00001770bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1771 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001772 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001773 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001774
Reid Spencerd84d35b2007-02-15 02:26:10 +00001775 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001776 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001777
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001778 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001779 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001780 return true;
1781}
1782
1783
Robert Bocchinoca27f032006-01-17 20:07:22 +00001784//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001785// ShuffleVectorInst Implementation
1786//===----------------------------------------------------------------------===//
1787
1788ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001789 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001790 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001791: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001792 cast<VectorType>(Mask->getType())->getNumElements()),
1793 ShuffleVector,
1794 OperandTraits<ShuffleVectorInst>::op_begin(this),
1795 OperandTraits<ShuffleVectorInst>::operands(this),
1796 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001797 assert(isValidOperands(V1, V2, Mask) &&
1798 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001799 Op<0>() = V1;
1800 Op<1>() = V2;
1801 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001802 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001803}
1804
1805ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001806 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001807 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001808: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1809 cast<VectorType>(Mask->getType())->getNumElements()),
1810 ShuffleVector,
1811 OperandTraits<ShuffleVectorInst>::op_begin(this),
1812 OperandTraits<ShuffleVectorInst>::operands(this),
1813 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001814 assert(isValidOperands(V1, V2, Mask) &&
1815 "Invalid shuffle vector instruction operands!");
1816
Gabor Greif2d3024d2008-05-26 21:33:52 +00001817 Op<0>() = V1;
1818 Op<1>() = V2;
1819 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001820 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001821}
1822
Mon P Wang25f01062008-11-10 04:46:22 +00001823bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001824 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001825 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001826 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001827 return false;
1828
Chris Lattner1dcb6542012-01-25 23:49:49 +00001829 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001830 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001831 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001832 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001833
1834 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001835 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1836 return true;
1837
Nate Begeman60a31c32010-08-13 00:16:46 +00001838 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001839 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001840 for (Value *Op : MV->operands()) {
1841 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001842 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001843 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001844 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001845 return false;
1846 }
1847 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001848 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001849 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001850
1851 if (const ConstantDataSequential *CDS =
1852 dyn_cast<ConstantDataSequential>(Mask)) {
1853 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1854 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1855 if (CDS->getElementAsInteger(i) >= V1Size*2)
1856 return false;
1857 return true;
1858 }
1859
1860 // The bitcode reader can create a place holder for a forward reference
1861 // used as the shuffle mask. When this occurs, the shuffle mask will
1862 // fall into this case and fail. To avoid this error, do this bit of
1863 // ugliness to allow such a mask pass.
1864 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1865 if (CE->getOpcode() == Instruction::UserOp1)
1866 return true;
1867
1868 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001869}
1870
Chris Lattnercf129702012-01-26 02:51:13 +00001871int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1872 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1873 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001874 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001875 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001876 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001877 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001878 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001879}
1880
Chris Lattnercf129702012-01-26 02:51:13 +00001881void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1882 SmallVectorImpl<int> &Result) {
1883 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001884
Chris Lattnercf129702012-01-26 02:51:13 +00001885 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001886 for (unsigned i = 0; i != NumElts; ++i)
1887 Result.push_back(CDS->getElementAsInteger(i));
1888 return;
1889 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001890 for (unsigned i = 0; i != NumElts; ++i) {
1891 Constant *C = Mask->getAggregateElement(i);
1892 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001893 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001894 }
1895}
1896
1897
Dan Gohman12fce772008-05-15 19:50:34 +00001898//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001899// InsertValueInst Class
1900//===----------------------------------------------------------------------===//
1901
Jay Foad57aa6362011-07-13 10:26:04 +00001902void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1903 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001904 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001905
1906 // There's no fundamental reason why we require at least one index
1907 // (other than weirdness with &*IdxBegin being invalid; see
1908 // getelementptr's init routine for example). But there's no
1909 // present need to support it.
1910 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1911
1912 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001913 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001914 Op<0>() = Agg;
1915 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001916
Jay Foad57aa6362011-07-13 10:26:04 +00001917 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001918 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001919}
1920
1921InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001922 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001923 OperandTraits<InsertValueInst>::op_begin(this), 2),
1924 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001925 Op<0>() = IVI.getOperand(0);
1926 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001927 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001928}
1929
1930//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001931// ExtractValueInst Class
1932//===----------------------------------------------------------------------===//
1933
Jay Foad57aa6362011-07-13 10:26:04 +00001934void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001935 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001936
Jay Foad57aa6362011-07-13 10:26:04 +00001937 // There's no fundamental reason why we require at least one index.
1938 // But there's no present need to support it.
1939 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001940
Jay Foad57aa6362011-07-13 10:26:04 +00001941 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001942 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001943}
1944
1945ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001946 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001947 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001948 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001949}
1950
Dan Gohman12fce772008-05-15 19:50:34 +00001951// getIndexedType - Returns the type of the element that would be extracted
1952// with an extractvalue instruction with the specified parameters.
1953//
1954// A null type is returned if the indices are invalid for the specified
1955// pointer type.
1956//
Chris Lattner229907c2011-07-18 04:54:35 +00001957Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001958 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001959 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001960 // We can't use CompositeType::indexValid(Index) here.
1961 // indexValid() always returns true for arrays because getelementptr allows
1962 // out-of-bounds indices. Since we don't allow those for extractvalue and
1963 // insertvalue we need to check array indexing manually.
1964 // Since the only other types we can index into are struct types it's just
1965 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001966 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001967 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001968 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001969 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001970 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001971 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001972 } else {
1973 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001974 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001975 }
1976
1977 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001978 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001979 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001980}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001981
1982//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001983// BinaryOperator Class
1984//===----------------------------------------------------------------------===//
1985
Chris Lattner2195fc42007-02-24 00:55:48 +00001986BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001987 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001988 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001989 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001990 OperandTraits<BinaryOperator>::op_begin(this),
1991 OperandTraits<BinaryOperator>::operands(this),
1992 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001993 Op<0>() = S1;
1994 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001995 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001996 setName(Name);
1997}
1998
1999BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002000 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002001 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002002 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002003 OperandTraits<BinaryOperator>::op_begin(this),
2004 OperandTraits<BinaryOperator>::operands(this),
2005 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002006 Op<0>() = S1;
2007 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002008 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002009 setName(Name);
2010}
2011
2012
2013void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002014 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002015 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002016 assert(LHS->getType() == RHS->getType() &&
2017 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002018#ifndef NDEBUG
2019 switch (iType) {
2020 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002021 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002022 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002023 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002024 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002025 "Tried to create an integer operation on a non-integer type!");
2026 break;
2027 case FAdd: case FSub:
2028 case FMul:
2029 assert(getType() == LHS->getType() &&
2030 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002031 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002032 "Tried to create a floating-point operation on a "
2033 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002034 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002035 case UDiv:
2036 case SDiv:
2037 assert(getType() == LHS->getType() &&
2038 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002039 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002040 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002041 "Incorrect operand type (not integer) for S/UDIV");
2042 break;
2043 case FDiv:
2044 assert(getType() == LHS->getType() &&
2045 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002046 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002047 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002048 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002049 case URem:
2050 case SRem:
2051 assert(getType() == LHS->getType() &&
2052 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002053 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002054 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002055 "Incorrect operand type (not integer) for S/UREM");
2056 break;
2057 case FRem:
2058 assert(getType() == LHS->getType() &&
2059 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002060 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002061 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002062 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002063 case Shl:
2064 case LShr:
2065 case AShr:
2066 assert(getType() == LHS->getType() &&
2067 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002068 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002069 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002070 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002071 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002072 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002073 case And: case Or:
2074 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002075 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002076 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002077 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002078 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002079 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002080 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002081 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002082 default:
2083 break;
2084 }
2085#endif
2086}
2087
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002088BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002089 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002090 Instruction *InsertBefore) {
2091 assert(S1->getType() == S2->getType() &&
2092 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002093 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002094}
2095
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002096BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002097 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002098 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002099 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002100 InsertAtEnd->getInstList().push_back(Res);
2101 return Res;
2102}
2103
Dan Gohman5476cfd2009-08-12 16:23:25 +00002104BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002105 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002106 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002107 return new BinaryOperator(Instruction::Sub,
2108 zero, Op,
2109 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002110}
2111
Dan Gohman5476cfd2009-08-12 16:23:25 +00002112BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002113 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002114 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002115 return new BinaryOperator(Instruction::Sub,
2116 zero, Op,
2117 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002118}
2119
Dan Gohman4ab44202009-12-18 02:58:50 +00002120BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2121 Instruction *InsertBefore) {
2122 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2123 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2124}
2125
2126BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2127 BasicBlock *InsertAtEnd) {
2128 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2129 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2130}
2131
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002132BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2133 Instruction *InsertBefore) {
2134 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2135 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2136}
2137
2138BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2139 BasicBlock *InsertAtEnd) {
2140 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2141 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2142}
2143
Dan Gohman5476cfd2009-08-12 16:23:25 +00002144BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002145 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002146 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002147 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002148 Op->getType(), Name, InsertBefore);
2149}
2150
Dan Gohman5476cfd2009-08-12 16:23:25 +00002151BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002152 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002153 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002154 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002155 Op->getType(), Name, InsertAtEnd);
2156}
2157
Dan Gohman5476cfd2009-08-12 16:23:25 +00002158BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002159 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002160 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002161 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002162 Op->getType(), Name, InsertBefore);
2163}
2164
Dan Gohman5476cfd2009-08-12 16:23:25 +00002165BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002166 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002167 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002168 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002169 Op->getType(), Name, InsertAtEnd);
2170}
2171
2172
2173// isConstantAllOnes - Helper function for several functions below
2174static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002175 if (const Constant *C = dyn_cast<Constant>(V))
2176 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002177 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002178}
2179
Owen Andersonbb2501b2009-07-13 22:18:28 +00002180bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002181 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2182 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002183 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002184 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002185 return false;
2186}
2187
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002188bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002189 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2190 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002191 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002192 if (!IgnoreZeroSign)
2193 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2194 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2195 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002196 return false;
2197}
2198
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002199bool BinaryOperator::isNot(const Value *V) {
2200 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2201 return (Bop->getOpcode() == Instruction::Xor &&
2202 (isConstantAllOnes(Bop->getOperand(1)) ||
2203 isConstantAllOnes(Bop->getOperand(0))));
2204 return false;
2205}
2206
Chris Lattner2c7d1772005-04-24 07:28:37 +00002207Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002208 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002209}
2210
Chris Lattner2c7d1772005-04-24 07:28:37 +00002211const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2212 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002213}
2214
Dan Gohmana5b96452009-06-04 22:49:04 +00002215Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002216 return cast<BinaryOperator>(BinOp)->getOperand(1);
2217}
2218
2219const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2220 return getFNegArgument(const_cast<Value*>(BinOp));
2221}
2222
Chris Lattner2c7d1772005-04-24 07:28:37 +00002223Value *BinaryOperator::getNotArgument(Value *BinOp) {
2224 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2225 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2226 Value *Op0 = BO->getOperand(0);
2227 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002228 if (isConstantAllOnes(Op0)) return Op1;
2229
2230 assert(isConstantAllOnes(Op1));
2231 return Op0;
2232}
2233
Chris Lattner2c7d1772005-04-24 07:28:37 +00002234const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2235 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002236}
2237
2238
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002239// Exchange the two operands to this instruction. This instruction is safe to
2240// use on any binary instruction and does not modify the semantics of the
2241// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2242// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002243bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002244 if (!isCommutative())
2245 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002246 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002247 return false;
2248}
2249
Sanjay Patela982d992014-09-03 01:06:50 +00002250
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002251//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002252// FPMathOperator Class
2253//===----------------------------------------------------------------------===//
2254
Duncan Sands05f4df82012-04-16 16:28:59 +00002255float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002256 const MDNode *MD =
2257 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002258 if (!MD)
2259 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002260 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002261 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002262}
2263
2264
2265//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002266// CastInst Class
2267//===----------------------------------------------------------------------===//
2268
David Blaikie3a15e142011-12-01 08:00:17 +00002269void CastInst::anchor() {}
2270
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271// Just determine if this cast only deals with integral->integral conversion.
2272bool CastInst::isIntegerCast() const {
2273 switch (getOpcode()) {
2274 default: return false;
2275 case Instruction::ZExt:
2276 case Instruction::SExt:
2277 case Instruction::Trunc:
2278 return true;
2279 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002280 return getOperand(0)->getType()->isIntegerTy() &&
2281 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002282 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002283}
2284
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002285bool CastInst::isLosslessCast() const {
2286 // Only BitCast can be lossless, exit fast if we're not BitCast
2287 if (getOpcode() != Instruction::BitCast)
2288 return false;
2289
2290 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002291 Type *SrcTy = getOperand(0)->getType();
2292 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293 if (SrcTy == DstTy)
2294 return true;
2295
Reid Spencer8d9336d2006-12-31 05:26:44 +00002296 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002297 if (SrcTy->isPointerTy())
2298 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 return false; // Other types have no identity values
2300}
2301
2302/// This function determines if the CastInst does not require any bits to be
2303/// changed in order to effect the cast. Essentially, it identifies cases where
2304/// no code gen is necessary for the cast, hence the name no-op cast. For
2305/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002306/// # bitcast i32* %x to i8*
2307/// # bitcast <2 x i32> %x to <4 x i16>
2308/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002309/// @brief Determine if the described cast is a no-op.
2310bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002311 Type *SrcTy,
2312 Type *DestTy,
2313 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002314 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002315 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316 case Instruction::Trunc:
2317 case Instruction::ZExt:
2318 case Instruction::SExt:
2319 case Instruction::FPTrunc:
2320 case Instruction::FPExt:
2321 case Instruction::UIToFP:
2322 case Instruction::SIToFP:
2323 case Instruction::FPToUI:
2324 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002325 case Instruction::AddrSpaceCast:
2326 // TODO: Target informations may give a more accurate answer here.
2327 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002328 case Instruction::BitCast:
2329 return true; // BitCast never modifies bits.
2330 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002331 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002332 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002333 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002334 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002335 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002336 }
2337}
2338
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002339/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002340bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002341 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2342}
2343
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002344bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002345 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002346 if (getOpcode() == Instruction::PtrToInt)
2347 PtrOpTy = getOperand(0)->getType();
2348 else if (getOpcode() == Instruction::IntToPtr)
2349 PtrOpTy = getType();
2350
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002351 Type *IntPtrTy =
2352 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002353
2354 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2355}
2356
2357/// This function determines if a pair of casts can be eliminated and what
2358/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359/// instructions like this:
2360/// * %F = firstOpcode SrcTy %x to MidTy
2361/// * %S = secondOpcode MidTy %F to DstTy
2362/// The function returns a resultOpcode so these two casts can be replaced with:
2363/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002364/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365unsigned CastInst::isEliminableCastPair(
2366 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002367 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2368 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369 // Define the 144 possibilities for these two cast instructions. The values
2370 // in this matrix determine what to do in a given situation and select the
2371 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002372 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373 // the following cast properties:
2374 //
2375 // Size Compare Source Destination
2376 // Operator Src ? Size Type Sign Type Sign
2377 // -------- ------------ ------------------- ---------------------
2378 // TRUNC > Integer Any Integral Any
2379 // ZEXT < Integral Unsigned Integer Any
2380 // SEXT < Integral Signed Integer Any
2381 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002382 // FPTOSI n/a FloatPt n/a Integral Signed
2383 // UITOFP n/a Integral Unsigned FloatPt n/a
2384 // SITOFP n/a Integral Signed FloatPt n/a
2385 // FPTRUNC > FloatPt n/a FloatPt n/a
2386 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387 // PTRTOINT n/a Pointer n/a Integral Unsigned
2388 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002389 // BITCAST = FirstClass n/a FirstClass n/a
2390 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002391 //
2392 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002393 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2394 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002395 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002396 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002397 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002398 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002399 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2401 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002402 // T F F U S F F P I B A -+
2403 // R Z S P P I I T P 2 N T S |
2404 // U E E 2 2 2 2 R E I T C C +- secondOp
2405 // N X X U S F F N X N 2 V V |
2406 // C T T I I P P C T T P T T -+
2407 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002408 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002409 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2410 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2411 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2412 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2413 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002414 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002415 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2416 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2417 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2418 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2419 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002421
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002422 // TODO: This logic could be encoded into the table above and handled in the
2423 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002424 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002425 // merging. However, any pair of bitcasts are allowed.
2426 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2427 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2428 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002429
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002430 // Check if any of the casts convert scalars <-> vectors.
2431 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2432 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2433 if (!AreBothBitcasts)
2434 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435
2436 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2437 [secondOp-Instruction::CastOpsBegin];
2438 switch (ElimCase) {
2439 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002440 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441 return 0;
2442 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002443 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 return firstOp;
2445 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002446 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447 return secondOp;
2448 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002449 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002450 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002451 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002452 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453 return firstOp;
2454 return 0;
2455 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002456 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002457 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002458 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459 return firstOp;
2460 return 0;
2461 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002462 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002463 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002464 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465 return secondOp;
2466 return 0;
2467 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002468 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002469 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002470 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 return secondOp;
2472 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002473 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002474 // Cannot simplify if address spaces are different!
2475 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2476 return 0;
2477
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002478 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002479 // We can still fold this without knowing the actual sizes as long we
2480 // know that the intermediate pointer is the largest possible
2481 // pointer size.
2482 // FIXME: Is this always true?
2483 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002484 return Instruction::BitCast;
2485
2486 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002487 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002488 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002489 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490 if (MidSize >= PtrSize)
2491 return Instruction::BitCast;
2492 return 0;
2493 }
2494 case 8: {
2495 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2496 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2497 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002498 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2499 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002500 if (SrcSize == DstSize)
2501 return Instruction::BitCast;
2502 else if (SrcSize < DstSize)
2503 return firstOp;
2504 return secondOp;
2505 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002506 case 9:
2507 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002508 return Instruction::ZExt;
2509 case 10:
2510 // fpext followed by ftrunc is allowed if the bit size returned to is
2511 // the same as the original, in which case its just a bitcast
2512 if (SrcTy == DstTy)
2513 return Instruction::BitCast;
2514 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002515 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002517 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002518 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002519 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002520 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2521 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522 if (SrcSize <= PtrSize && SrcSize == DstSize)
2523 return Instruction::BitCast;
2524 return 0;
2525 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002526 case 12: {
2527 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2528 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2529 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2530 return Instruction::AddrSpaceCast;
2531 return Instruction::BitCast;
2532 }
2533 case 13:
2534 // FIXME: this state can be merged with (1), but the following assert
2535 // is useful to check the correcteness of the sequence due to semantic
2536 // change of bitcast.
2537 assert(
2538 SrcTy->isPtrOrPtrVectorTy() &&
2539 MidTy->isPtrOrPtrVectorTy() &&
2540 DstTy->isPtrOrPtrVectorTy() &&
2541 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2542 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2543 "Illegal addrspacecast, bitcast sequence!");
2544 // Allowed, use first cast's opcode
2545 return firstOp;
2546 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002547 // bitcast, addrspacecast -> addrspacecast if the element type of
2548 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002549 if (SrcTy->getScalarType()->getPointerElementType() ==
2550 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002551 return Instruction::AddrSpaceCast;
2552 return 0;
2553
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002554 case 15:
2555 // FIXME: this state can be merged with (1), but the following assert
2556 // is useful to check the correcteness of the sequence due to semantic
2557 // change of bitcast.
2558 assert(
2559 SrcTy->isIntOrIntVectorTy() &&
2560 MidTy->isPtrOrPtrVectorTy() &&
2561 DstTy->isPtrOrPtrVectorTy() &&
2562 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2563 "Illegal inttoptr, bitcast sequence!");
2564 // Allowed, use first cast's opcode
2565 return firstOp;
2566 case 16:
2567 // FIXME: this state can be merged with (2), but the following assert
2568 // is useful to check the correcteness of the sequence due to semantic
2569 // change of bitcast.
2570 assert(
2571 SrcTy->isPtrOrPtrVectorTy() &&
2572 MidTy->isPtrOrPtrVectorTy() &&
2573 DstTy->isIntOrIntVectorTy() &&
2574 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2575 "Illegal bitcast, ptrtoint sequence!");
2576 // Allowed, use second cast's opcode
2577 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002578 case 17:
2579 // (sitofp (zext x)) -> (uitofp x)
2580 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002582 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002584 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585 default:
Craig Topperc514b542012-02-05 22:14:15 +00002586 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588}
2589
Chris Lattner229907c2011-07-18 04:54:35 +00002590CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002591 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002592 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593 // Construct and return the appropriate CastInst subclass
2594 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002595 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2596 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2597 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2598 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2599 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2600 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2601 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2602 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2603 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2604 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2605 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2606 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2607 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2608 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610}
2611
Chris Lattner229907c2011-07-18 04:54:35 +00002612CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002613 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002614 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615 // Construct and return the appropriate CastInst subclass
2616 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002617 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2618 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2619 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2620 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2621 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2622 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2623 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2624 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2625 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2626 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2627 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2628 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2629 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2630 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002632}
2633
Chris Lattner229907c2011-07-18 04:54:35 +00002634CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002635 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002636 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002637 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002638 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2639 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002640}
2641
Chris Lattner229907c2011-07-18 04:54:35 +00002642CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002643 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002644 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002645 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002646 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2647 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002648}
2649
Chris Lattner229907c2011-07-18 04:54:35 +00002650CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002651 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002652 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002653 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002654 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2655 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002656}
2657
Chris Lattner229907c2011-07-18 04:54:35 +00002658CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002659 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002660 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002661 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002662 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2663 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002664}
2665
Chris Lattner229907c2011-07-18 04:54:35 +00002666CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002667 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002668 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002669 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002670 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2671 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002672}
2673
Chris Lattner229907c2011-07-18 04:54:35 +00002674CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002675 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002676 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002677 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002678 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2679 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002680}
2681
Chris Lattner229907c2011-07-18 04:54:35 +00002682CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002683 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002684 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002685 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2686 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2687 "Invalid cast");
2688 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002689 assert((!Ty->isVectorTy() ||
2690 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002691 "Invalid cast");
2692
Matt Arsenault065ced92013-07-31 00:17:33 +00002693 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002694 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002695
Matt Arsenault740980e2014-07-14 17:24:35 +00002696 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002697}
2698
2699/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002700CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2701 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002702 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002703 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2704 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002705 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002706 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002707 assert((!Ty->isVectorTy() ||
2708 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002709 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002710
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002711 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002712 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002713
Matt Arsenault740980e2014-07-14 17:24:35 +00002714 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2715}
2716
2717CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2718 Value *S, Type *Ty,
2719 const Twine &Name,
2720 BasicBlock *InsertAtEnd) {
2721 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2722 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2723
2724 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2725 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2726
2727 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2728}
2729
2730CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2731 Value *S, Type *Ty,
2732 const Twine &Name,
2733 Instruction *InsertBefore) {
2734 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2735 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2736
2737 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002738 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2739
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002740 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002741}
2742
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002743CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2744 const Twine &Name,
2745 Instruction *InsertBefore) {
2746 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2747 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2748 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2749 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2750
2751 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2752}
2753
Matt Arsenault740980e2014-07-14 17:24:35 +00002754CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002755 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002756 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002757 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002758 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002759 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2760 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002761 Instruction::CastOps opcode =
2762 (SrcBits == DstBits ? Instruction::BitCast :
2763 (SrcBits > DstBits ? Instruction::Trunc :
2764 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002765 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002766}
2767
Chris Lattner229907c2011-07-18 04:54:35 +00002768CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002769 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002770 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002771 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002772 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002773 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2774 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002775 Instruction::CastOps opcode =
2776 (SrcBits == DstBits ? Instruction::BitCast :
2777 (SrcBits > DstBits ? Instruction::Trunc :
2778 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002779 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002780}
2781
Chris Lattner229907c2011-07-18 04:54:35 +00002782CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002783 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002784 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002785 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002786 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002787 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2788 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002789 Instruction::CastOps opcode =
2790 (SrcBits == DstBits ? Instruction::BitCast :
2791 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002792 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002793}
2794
Chris Lattner229907c2011-07-18 04:54:35 +00002795CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002796 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002797 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002798 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002799 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002800 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2801 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002802 Instruction::CastOps opcode =
2803 (SrcBits == DstBits ? Instruction::BitCast :
2804 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002805 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002806}
2807
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002808// Check whether it is valid to call getCastOpcode for these types.
2809// This routine must be kept in sync with getCastOpcode.
2810bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2811 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2812 return false;
2813
2814 if (SrcTy == DestTy)
2815 return true;
2816
2817 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2818 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2819 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2820 // An element by element cast. Valid if casting the elements is valid.
2821 SrcTy = SrcVecTy->getElementType();
2822 DestTy = DestVecTy->getElementType();
2823 }
2824
2825 // Get the bit sizes, we'll need these
2826 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2827 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2828
2829 // Run through the possibilities ...
2830 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002831 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002832 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002833 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002834 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002835 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002836 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002837 // Casting from something else
2838 return SrcTy->isPointerTy();
2839 }
2840 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2841 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002842 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002843 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002844 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002845 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002846 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002847 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002848 return false;
2849 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002850 if (DestTy->isVectorTy()) // Casting to vector
2851 return DestBits == SrcBits;
2852 if (DestTy->isPointerTy()) { // Casting to pointer
2853 if (SrcTy->isPointerTy()) // Casting from pointer
2854 return true;
2855 return SrcTy->isIntegerTy(); // Casting from integral
2856 }
2857 if (DestTy->isX86_MMXTy()) {
2858 if (SrcTy->isVectorTy())
2859 return DestBits == SrcBits; // 64-bit vector to MMX
2860 return false;
2861 } // Casting to something else
2862 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002863}
2864
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002865bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2866 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2867 return false;
2868
2869 if (SrcTy == DestTy)
2870 return true;
2871
2872 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2873 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2874 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2875 // An element by element cast. Valid if casting the elements is valid.
2876 SrcTy = SrcVecTy->getElementType();
2877 DestTy = DestVecTy->getElementType();
2878 }
2879 }
2880 }
2881
2882 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2883 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2884 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2885 }
2886 }
2887
2888 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2889 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2890
2891 // Could still have vectors of pointers if the number of elements doesn't
2892 // match
2893 if (SrcBits == 0 || DestBits == 0)
2894 return false;
2895
2896 if (SrcBits != DestBits)
2897 return false;
2898
2899 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2900 return false;
2901
2902 return true;
2903}
2904
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002905bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002906 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002907 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2908 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002909 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002910 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2911 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002912 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002913
2914 return isBitCastable(SrcTy, DestTy);
2915}
2916
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002917// Provide a way to get a "cast" where the cast opcode is inferred from the
2918// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002919// logic in the castIsValid function below. This axiom should hold:
2920// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2921// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002922// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002923// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002924Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002925CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002926 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2927 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002928
Duncan Sands55e50902008-01-06 10:12:28 +00002929 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2930 "Only first class types are castable!");
2931
Duncan Sandsa8514532011-05-18 07:13:41 +00002932 if (SrcTy == DestTy)
2933 return BitCast;
2934
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002935 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002936 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2937 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002938 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2939 // An element by element cast. Find the appropriate opcode based on the
2940 // element types.
2941 SrcTy = SrcVecTy->getElementType();
2942 DestTy = DestVecTy->getElementType();
2943 }
2944
2945 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002946 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2947 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002948
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002949 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002950 if (DestTy->isIntegerTy()) { // Casting to integral
2951 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002952 if (DestBits < SrcBits)
2953 return Trunc; // int -> smaller int
2954 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002955 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002956 return SExt; // signed -> SEXT
2957 else
2958 return ZExt; // unsigned -> ZEXT
2959 } else {
2960 return BitCast; // Same size, No-op cast
2961 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002962 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002963 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002964 return FPToSI; // FP -> sint
2965 else
2966 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002967 } else if (SrcTy->isVectorTy()) {
2968 assert(DestBits == SrcBits &&
2969 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002970 return BitCast; // Same size, no-op cast
2971 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002972 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002973 "Casting from a value that is not first-class type");
2974 return PtrToInt; // ptr -> int
2975 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002976 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2977 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002978 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002979 return SIToFP; // sint -> FP
2980 else
2981 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002982 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002983 if (DestBits < SrcBits) {
2984 return FPTrunc; // FP -> smaller FP
2985 } else if (DestBits > SrcBits) {
2986 return FPExt; // FP -> larger FP
2987 } else {
2988 return BitCast; // same size, no-op cast
2989 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002990 } else if (SrcTy->isVectorTy()) {
2991 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002992 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002993 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002994 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002995 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002996 } else if (DestTy->isVectorTy()) {
2997 assert(DestBits == SrcBits &&
2998 "Illegal cast to vector (wrong type or size)");
2999 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003000 } else if (DestTy->isPointerTy()) {
3001 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003002 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3003 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003004 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003005 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003006 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003007 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003008 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003009 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003010 if (SrcTy->isVectorTy()) {
3011 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003012 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003013 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003014 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003015 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003016 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017}
3018
3019//===----------------------------------------------------------------------===//
3020// CastInst SubClass Constructors
3021//===----------------------------------------------------------------------===//
3022
3023/// Check that the construction parameters for a CastInst are correct. This
3024/// could be broken out into the separate constructors but it is useful to have
3025/// it in one place and to eliminate the redundant code for getting the sizes
3026/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003027bool
Chris Lattner229907c2011-07-18 04:54:35 +00003028CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003029
3030 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003031 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003032
Chris Lattner37bc78a2010-01-26 21:51:43 +00003033 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3034 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003035 return false;
3036
3037 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003038 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3039 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003040
Duncan Sands7f646562011-05-18 09:21:57 +00003041 // If these are vector types, get the lengths of the vectors (using zero for
3042 // scalar types means that checking that vector lengths match also checks that
3043 // scalars are not being converted to vectors or vectors to scalars).
3044 unsigned SrcLength = SrcTy->isVectorTy() ?
3045 cast<VectorType>(SrcTy)->getNumElements() : 0;
3046 unsigned DstLength = DstTy->isVectorTy() ?
3047 cast<VectorType>(DstTy)->getNumElements() : 0;
3048
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003049 // Switch on the opcode provided
3050 switch (op) {
3051 default: return false; // This is an input error
3052 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003053 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3054 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003055 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003056 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3057 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003058 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003059 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3060 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003061 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003062 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3063 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003064 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003065 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3066 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003067 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003068 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003069 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3070 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003071 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003072 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003073 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3074 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003075 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003076 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003077 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003078 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3079 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3080 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003081 return SrcTy->getScalarType()->isPointerTy() &&
3082 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003083 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003084 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003085 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003086 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3087 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3088 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003089 return SrcTy->getScalarType()->isIntegerTy() &&
3090 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003091 case Instruction::BitCast: {
3092 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3093 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3094
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003095 // BitCast implies a no-op cast of type only. No bits change.
3096 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003097 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003098 return false;
3099
Alp Tokerf907b892013-12-05 05:44:44 +00003100 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003101 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003102 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003103 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3104
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003105 // If both are pointers then the address spaces must match.
3106 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3107 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003108
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003109 // A vector of pointers must have the same number of elements.
3110 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3111 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3112 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3113
3114 return false;
3115 }
3116
3117 return true;
3118 }
3119 case Instruction::AddrSpaceCast: {
3120 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3121 if (!SrcPtrTy)
3122 return false;
3123
3124 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3125 if (!DstPtrTy)
3126 return false;
3127
3128 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3129 return false;
3130
3131 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3132 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3133 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3134
3135 return false;
3136 }
3137
3138 return true;
3139 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003140 }
3141}
3142
3143TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003144 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003145) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003146 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003147}
3148
3149TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003150 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003151) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003152 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003153}
3154
3155ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003156 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003157) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003158 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003159}
3160
3161ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003162 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003163) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003164 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003165}
3166SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003167 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003168) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003169 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003170}
3171
Jeff Cohencc08c832006-12-02 02:22:01 +00003172SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003173 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003174) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003175 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003176}
3177
3178FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003179 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003180) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003181 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003182}
3183
3184FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003185 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003186) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003187 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003188}
3189
3190FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003191 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003192) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003193 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003194}
3195
3196FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003197 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003198) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003199 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003200}
3201
3202UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003203 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003204) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003205 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003206}
3207
3208UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003209 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003210) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003211 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003212}
3213
3214SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003215 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003216) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003217 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003218}
3219
3220SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003221 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003222) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003223 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003224}
3225
3226FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003227 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003229 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003230}
3231
3232FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003233 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003235 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003236}
3237
3238FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003239 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003240) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003241 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003242}
3243
3244FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003245 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003246) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003247 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003248}
3249
3250PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003251 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003252) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003253 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003254}
3255
3256PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003257 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003258) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003259 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003260}
3261
3262IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003263 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003265 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003266}
3267
3268IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003269 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003271 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003272}
3273
3274BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003275 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003277 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003278}
3279
3280BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003281 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003282) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003283 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003284}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003285
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003286AddrSpaceCastInst::AddrSpaceCastInst(
3287 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3288) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3289 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3290}
3291
3292AddrSpaceCastInst::AddrSpaceCastInst(
3293 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3294) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3295 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3296}
3297
Chris Lattnerf16dc002006-09-17 19:29:56 +00003298//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003299// CmpInst Classes
3300//===----------------------------------------------------------------------===//
3301
Craig Topper3186c012012-09-23 02:12:10 +00003302void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003303
Craig Topper1c3f2832015-12-15 06:11:33 +00003304CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3305 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003306 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003307 OperandTraits<CmpInst>::op_begin(this),
3308 OperandTraits<CmpInst>::operands(this),
3309 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003310 Op<0>() = LHS;
3311 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003312 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003313 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003314}
Gabor Greiff6caff662008-05-10 08:32:32 +00003315
Craig Topper1c3f2832015-12-15 06:11:33 +00003316CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3317 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003318 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003319 OperandTraits<CmpInst>::op_begin(this),
3320 OperandTraits<CmpInst>::operands(this),
3321 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003322 Op<0>() = LHS;
3323 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003324 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003325 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003326}
3327
3328CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003329CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003330 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003331 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003332 if (InsertBefore)
3333 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3334 S1, S2, Name);
3335 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003336 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003337 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003338 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003339
3340 if (InsertBefore)
3341 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3342 S1, S2, Name);
3343 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003344 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003345 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003346}
3347
3348CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003349CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003350 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003351 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003352 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3353 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003354 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003355 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3356 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003357}
3358
3359void CmpInst::swapOperands() {
3360 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3361 IC->swapOperands();
3362 else
3363 cast<FCmpInst>(this)->swapOperands();
3364}
3365
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003366bool CmpInst::isCommutative() const {
3367 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003368 return IC->isCommutative();
3369 return cast<FCmpInst>(this)->isCommutative();
3370}
3371
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003372bool CmpInst::isEquality() const {
3373 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003374 return IC->isEquality();
3375 return cast<FCmpInst>(this)->isEquality();
3376}
3377
3378
Dan Gohman4e724382008-05-31 02:47:54 +00003379CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003380 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003381 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003382 case ICMP_EQ: return ICMP_NE;
3383 case ICMP_NE: return ICMP_EQ;
3384 case ICMP_UGT: return ICMP_ULE;
3385 case ICMP_ULT: return ICMP_UGE;
3386 case ICMP_UGE: return ICMP_ULT;
3387 case ICMP_ULE: return ICMP_UGT;
3388 case ICMP_SGT: return ICMP_SLE;
3389 case ICMP_SLT: return ICMP_SGE;
3390 case ICMP_SGE: return ICMP_SLT;
3391 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003392
Dan Gohman4e724382008-05-31 02:47:54 +00003393 case FCMP_OEQ: return FCMP_UNE;
3394 case FCMP_ONE: return FCMP_UEQ;
3395 case FCMP_OGT: return FCMP_ULE;
3396 case FCMP_OLT: return FCMP_UGE;
3397 case FCMP_OGE: return FCMP_ULT;
3398 case FCMP_OLE: return FCMP_UGT;
3399 case FCMP_UEQ: return FCMP_ONE;
3400 case FCMP_UNE: return FCMP_OEQ;
3401 case FCMP_UGT: return FCMP_OLE;
3402 case FCMP_ULT: return FCMP_OGE;
3403 case FCMP_UGE: return FCMP_OLT;
3404 case FCMP_ULE: return FCMP_OGT;
3405 case FCMP_ORD: return FCMP_UNO;
3406 case FCMP_UNO: return FCMP_ORD;
3407 case FCMP_TRUE: return FCMP_FALSE;
3408 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003409 }
3410}
3411
Tim Northoverde3aea0412016-08-17 20:25:25 +00003412StringRef CmpInst::getPredicateName(Predicate Pred) {
3413 switch (Pred) {
3414 default: return "unknown";
3415 case FCmpInst::FCMP_FALSE: return "false";
3416 case FCmpInst::FCMP_OEQ: return "oeq";
3417 case FCmpInst::FCMP_OGT: return "ogt";
3418 case FCmpInst::FCMP_OGE: return "oge";
3419 case FCmpInst::FCMP_OLT: return "olt";
3420 case FCmpInst::FCMP_OLE: return "ole";
3421 case FCmpInst::FCMP_ONE: return "one";
3422 case FCmpInst::FCMP_ORD: return "ord";
3423 case FCmpInst::FCMP_UNO: return "uno";
3424 case FCmpInst::FCMP_UEQ: return "ueq";
3425 case FCmpInst::FCMP_UGT: return "ugt";
3426 case FCmpInst::FCMP_UGE: return "uge";
3427 case FCmpInst::FCMP_ULT: return "ult";
3428 case FCmpInst::FCMP_ULE: return "ule";
3429 case FCmpInst::FCMP_UNE: return "une";
3430 case FCmpInst::FCMP_TRUE: return "true";
3431 case ICmpInst::ICMP_EQ: return "eq";
3432 case ICmpInst::ICMP_NE: return "ne";
3433 case ICmpInst::ICMP_SGT: return "sgt";
3434 case ICmpInst::ICMP_SGE: return "sge";
3435 case ICmpInst::ICMP_SLT: return "slt";
3436 case ICmpInst::ICMP_SLE: return "sle";
3437 case ICmpInst::ICMP_UGT: return "ugt";
3438 case ICmpInst::ICMP_UGE: return "uge";
3439 case ICmpInst::ICMP_ULT: return "ult";
3440 case ICmpInst::ICMP_ULE: return "ule";
3441 }
3442}
3443
Pete Cooper1d078692015-12-14 20:29:16 +00003444void ICmpInst::anchor() {}
3445
Reid Spencer266e42b2006-12-23 06:05:41 +00003446ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3447 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003448 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003449 case ICMP_EQ: case ICMP_NE:
3450 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3451 return pred;
3452 case ICMP_UGT: return ICMP_SGT;
3453 case ICMP_ULT: return ICMP_SLT;
3454 case ICMP_UGE: return ICMP_SGE;
3455 case ICMP_ULE: return ICMP_SLE;
3456 }
3457}
3458
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003459ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3460 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003461 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003462 case ICMP_EQ: case ICMP_NE:
3463 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3464 return pred;
3465 case ICMP_SGT: return ICMP_UGT;
3466 case ICMP_SLT: return ICMP_ULT;
3467 case ICMP_SGE: return ICMP_UGE;
3468 case ICMP_SLE: return ICMP_ULE;
3469 }
3470}
3471
Dan Gohman4e724382008-05-31 02:47:54 +00003472CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003473 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003474 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003475 case ICMP_EQ: case ICMP_NE:
3476 return pred;
3477 case ICMP_SGT: return ICMP_SLT;
3478 case ICMP_SLT: return ICMP_SGT;
3479 case ICMP_SGE: return ICMP_SLE;
3480 case ICMP_SLE: return ICMP_SGE;
3481 case ICMP_UGT: return ICMP_ULT;
3482 case ICMP_ULT: return ICMP_UGT;
3483 case ICMP_UGE: return ICMP_ULE;
3484 case ICMP_ULE: return ICMP_UGE;
3485
Reid Spencerd9436b62006-11-20 01:22:35 +00003486 case FCMP_FALSE: case FCMP_TRUE:
3487 case FCMP_OEQ: case FCMP_ONE:
3488 case FCMP_UEQ: case FCMP_UNE:
3489 case FCMP_ORD: case FCMP_UNO:
3490 return pred;
3491 case FCMP_OGT: return FCMP_OLT;
3492 case FCMP_OLT: return FCMP_OGT;
3493 case FCMP_OGE: return FCMP_OLE;
3494 case FCMP_OLE: return FCMP_OGE;
3495 case FCMP_UGT: return FCMP_ULT;
3496 case FCMP_ULT: return FCMP_UGT;
3497 case FCMP_UGE: return FCMP_ULE;
3498 case FCMP_ULE: return FCMP_UGE;
3499 }
3500}
3501
Sanjoy Das6e78b172015-10-22 19:57:34 +00003502CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3503 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3504
3505 switch (pred) {
3506 default:
3507 llvm_unreachable("Unknown predicate!");
3508 case CmpInst::ICMP_ULT:
3509 return CmpInst::ICMP_SLT;
3510 case CmpInst::ICMP_ULE:
3511 return CmpInst::ICMP_SLE;
3512 case CmpInst::ICMP_UGT:
3513 return CmpInst::ICMP_SGT;
3514 case CmpInst::ICMP_UGE:
3515 return CmpInst::ICMP_SGE;
3516 }
3517}
3518
Craig Topper1c3f2832015-12-15 06:11:33 +00003519bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003520 switch (predicate) {
3521 default: return false;
3522 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3523 case ICmpInst::ICMP_UGE: return true;
3524 }
3525}
3526
Craig Topper1c3f2832015-12-15 06:11:33 +00003527bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003528 switch (predicate) {
3529 default: return false;
3530 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3531 case ICmpInst::ICMP_SGE: return true;
3532 }
3533}
3534
Craig Topper1c3f2832015-12-15 06:11:33 +00003535bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003536 switch (predicate) {
3537 default: return false;
3538 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3539 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3540 case FCmpInst::FCMP_ORD: return true;
3541 }
3542}
3543
Craig Topper1c3f2832015-12-15 06:11:33 +00003544bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003545 switch (predicate) {
3546 default: return false;
3547 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3548 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3549 case FCmpInst::FCMP_UNO: return true;
3550 }
3551}
3552
Craig Topper1c3f2832015-12-15 06:11:33 +00003553bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003554 switch(predicate) {
3555 default: return false;
3556 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3557 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3558 }
3559}
3560
Craig Topper1c3f2832015-12-15 06:11:33 +00003561bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003562 switch(predicate) {
3563 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3564 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3565 default: return false;
3566 }
3567}
3568
Chad Rosier99bc4802016-04-21 16:18:02 +00003569bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003570 // If the predicates match, then we know the first condition implies the
3571 // second is true.
3572 if (Pred1 == Pred2)
3573 return true;
3574
3575 switch (Pred1) {
3576 default:
3577 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003578 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003579 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
Chad Rosier1a601592016-04-22 17:57:34 +00003580 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3581 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003582 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3583 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3584 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3585 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3586 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3587 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3588 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3589 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003590 }
3591 return false;
3592}
3593
Chad Rosier99bc4802016-04-21 16:18:02 +00003594bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003595 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003596}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003597
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003598//===----------------------------------------------------------------------===//
3599// SwitchInst Implementation
3600//===----------------------------------------------------------------------===//
3601
Chris Lattnerbaf00152010-11-17 05:41:46 +00003602void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3603 assert(Value && Default && NumReserved);
3604 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003605 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003606 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003607
Pete Coopereb31b682015-05-21 22:48:54 +00003608 Op<0>() = Value;
3609 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003610}
3611
Chris Lattner2195fc42007-02-24 00:55:48 +00003612/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3613/// switch on and a default destination. The number of additional cases can
3614/// be specified here to make memory allocation more efficient. This
3615/// constructor can also autoinsert before another instruction.
3616SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3617 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003618 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003619 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003620 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003621}
3622
3623/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3624/// switch on and a default destination. The number of additional cases can
3625/// be specified here to make memory allocation more efficient. This
3626/// constructor also autoinserts at the end of the specified BasicBlock.
3627SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3628 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003629 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003630 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003631 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003632}
3633
Misha Brukmanb1c93172005-04-21 23:48:37 +00003634SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003635 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003636 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003637 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003638 Use *OL = getOperandList();
3639 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003640 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003641 OL[i] = InOL[i];
3642 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003643 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003644 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003645}
3646
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003647
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003648/// addCase - Add an entry to the switch instruction...
3649///
Chris Lattner47ac1872005-02-24 05:32:09 +00003650void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003651 unsigned NewCaseIdx = getNumCases();
3652 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003653 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003654 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003655 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003656 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003657 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003658 CaseIt Case(this, NewCaseIdx);
3659 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003660 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003661}
3662
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003663/// removeCase - This method removes the specified case and its successor
3664/// from the switch instruction.
Chandler Carruth0d256c02017-03-26 02:49:23 +00003665SwitchInst::CaseIt SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003666 unsigned idx = i.getCaseIndex();
3667
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003668 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003669
3670 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003671 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003672
Jay Foad14277722011-02-01 09:22:34 +00003673 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003674 if (2 + (idx + 1) * 2 != NumOps) {
3675 OL[2 + idx * 2] = OL[NumOps - 2];
3676 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003677 }
3678
3679 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003680 OL[NumOps-2].set(nullptr);
3681 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003682 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003683
3684 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003685}
3686
Jay Foade98f29d2011-04-01 08:00:58 +00003687/// growOperands - grow operands - This grows the operand list in response
3688/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003689///
Jay Foade98f29d2011-04-01 08:00:58 +00003690void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003691 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003692 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003693
3694 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003695 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003696}
3697
3698
3699BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3700 return getSuccessor(idx);
3701}
3702unsigned SwitchInst::getNumSuccessorsV() const {
3703 return getNumSuccessors();
3704}
3705void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3706 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003707}
Chris Lattnerf22be932004-10-15 23:52:53 +00003708
Chris Lattner3ed871f2009-10-27 19:13:16 +00003709//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003710// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003711//===----------------------------------------------------------------------===//
3712
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003713void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003714 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003715 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003716 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003717 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003718 allocHungoffUses(ReservedSpace);
3719
Pete Coopereb31b682015-05-21 22:48:54 +00003720 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003721}
3722
3723
Jay Foade98f29d2011-04-01 08:00:58 +00003724/// growOperands - grow operands - This grows the operand list in response
3725/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003726///
Jay Foade98f29d2011-04-01 08:00:58 +00003727void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003728 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003729 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003730
3731 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003732 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003733}
3734
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003735IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3736 Instruction *InsertBefore)
3737: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003738 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003739 init(Address, NumCases);
3740}
3741
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003742IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3743 BasicBlock *InsertAtEnd)
3744: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003745 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003746 init(Address, NumCases);
3747}
3748
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003749IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003750 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3751 nullptr, IBI.getNumOperands()) {
3752 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003753 Use *OL = getOperandList();
3754 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003755 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3756 OL[i] = InOL[i];
3757 SubclassOptionalData = IBI.SubclassOptionalData;
3758}
3759
Chris Lattner3ed871f2009-10-27 19:13:16 +00003760/// addDestination - Add a destination.
3761///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003762void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003763 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003764 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003765 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003766 // Initialize some new operands.
3767 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003768 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003769 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003770}
3771
3772/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003773/// indirectbr instruction.
3774void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003775 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3776
3777 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003778 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003779
3780 // Replace this value with the last one.
3781 OL[idx+1] = OL[NumOps-1];
3782
3783 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003784 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003785 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003786}
3787
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003788BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003789 return getSuccessor(idx);
3790}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003791unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003792 return getNumSuccessors();
3793}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003794void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003795 setSuccessor(idx, B);
3796}
3797
3798//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003799// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003800//===----------------------------------------------------------------------===//
3801
Chris Lattnerf22be932004-10-15 23:52:53 +00003802// Define these methods here so vtables don't get emitted into every translation
3803// unit that uses these classes.
3804
Pete Cooper75403d72015-06-24 20:22:23 +00003805GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003806 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003807}
3808
Pete Cooper75403d72015-06-24 20:22:23 +00003809BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003810 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003811}
3812
Pete Cooper75403d72015-06-24 20:22:23 +00003813FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003814 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003815}
3816
Pete Cooper75403d72015-06-24 20:22:23 +00003817ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003818 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003819}
3820
Pete Cooper75403d72015-06-24 20:22:23 +00003821ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003822 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003823}
3824
Pete Cooper75403d72015-06-24 20:22:23 +00003825InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003826 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003827}
3828
Pete Cooper75403d72015-06-24 20:22:23 +00003829AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003830 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3831 (Value *)getOperand(0), getAlignment());
3832 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003833 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003834 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003835}
3836
Pete Cooper75403d72015-06-24 20:22:23 +00003837LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003838 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3839 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003840}
3841
Pete Cooper75403d72015-06-24 20:22:23 +00003842StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003843 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003844 getAlignment(), getOrdering(), getSynchScope());
3845
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003846}
3847
Pete Cooper75403d72015-06-24 20:22:23 +00003848AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003849 AtomicCmpXchgInst *Result =
3850 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003851 getSuccessOrdering(), getFailureOrdering(),
3852 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003853 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003854 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003855 return Result;
3856}
3857
Pete Cooper75403d72015-06-24 20:22:23 +00003858AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003859 AtomicRMWInst *Result =
3860 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3861 getOrdering(), getSynchScope());
3862 Result->setVolatile(isVolatile());
3863 return Result;
3864}
3865
Pete Cooper75403d72015-06-24 20:22:23 +00003866FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003867 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3868}
3869
Pete Cooper75403d72015-06-24 20:22:23 +00003870TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003871 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003872}
3873
Pete Cooper75403d72015-06-24 20:22:23 +00003874ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003875 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003876}
3877
Pete Cooper75403d72015-06-24 20:22:23 +00003878SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003879 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003880}
3881
Pete Cooper75403d72015-06-24 20:22:23 +00003882FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003883 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003884}
3885
Pete Cooper75403d72015-06-24 20:22:23 +00003886FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003887 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003888}
3889
Pete Cooper75403d72015-06-24 20:22:23 +00003890UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003891 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003892}
3893
Pete Cooper75403d72015-06-24 20:22:23 +00003894SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003895 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003896}
3897
Pete Cooper75403d72015-06-24 20:22:23 +00003898FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003899 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003900}
3901
Pete Cooper75403d72015-06-24 20:22:23 +00003902FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003903 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003904}
3905
Pete Cooper75403d72015-06-24 20:22:23 +00003906PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003907 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003908}
3909
Pete Cooper75403d72015-06-24 20:22:23 +00003910IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003911 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003912}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003913
Pete Cooper75403d72015-06-24 20:22:23 +00003914BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003915 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003916}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003917
Pete Cooper75403d72015-06-24 20:22:23 +00003918AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003919 return new AddrSpaceCastInst(getOperand(0), getType());
3920}
3921
Pete Cooper75403d72015-06-24 20:22:23 +00003922CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003923 if (hasOperandBundles()) {
3924 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3925 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3926 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003927 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003928}
3929
Pete Cooper75403d72015-06-24 20:22:23 +00003930SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003931 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003932}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003933
Pete Cooper75403d72015-06-24 20:22:23 +00003934VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003935 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003936}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003937
Pete Cooper75403d72015-06-24 20:22:23 +00003938ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003939 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003940}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003941
Pete Cooper75403d72015-06-24 20:22:23 +00003942InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003943 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003944}
3945
Pete Cooper75403d72015-06-24 20:22:23 +00003946ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003947 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003948}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003949
Pete Cooper75403d72015-06-24 20:22:23 +00003950PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003951
Pete Cooper75403d72015-06-24 20:22:23 +00003952LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003953 return new LandingPadInst(*this);
3954}
3955
Pete Cooper75403d72015-06-24 20:22:23 +00003956ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003957 return new(getNumOperands()) ReturnInst(*this);
3958}
3959
Pete Cooper75403d72015-06-24 20:22:23 +00003960BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003961 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003962}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003963
Pete Cooper75403d72015-06-24 20:22:23 +00003964SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003965
Pete Cooper75403d72015-06-24 20:22:23 +00003966IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003967 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003968}
3969
Pete Cooper75403d72015-06-24 20:22:23 +00003970InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003971 if (hasOperandBundles()) {
3972 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3973 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3974 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003975 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003976}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003977
Pete Cooper75403d72015-06-24 20:22:23 +00003978ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003979
David Majnemer654e1302015-07-31 17:58:14 +00003980CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3981 return new (getNumOperands()) CleanupReturnInst(*this);
3982}
3983
David Majnemer654e1302015-07-31 17:58:14 +00003984CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003985 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003986}
3987
David Majnemer8a1c45d2015-12-12 05:38:55 +00003988CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3989 return new CatchSwitchInst(*this);
3990}
3991
3992FuncletPadInst *FuncletPadInst::cloneImpl() const {
3993 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003994}
3995
Pete Cooper75403d72015-06-24 20:22:23 +00003996UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003997 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003998 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003999}